Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
How can I execute one for-loop function, then call another function after the for-loop function is complete with a button?

I am trying to run a function after parsing some CSV with Papaparse on button click. I can only make it work with two buttons which is not the best. I actually have some more code that will update the selected_csv array with push() method.

What I want to do is feed the selected-csv array to Papaparse for-loop and push() the results to combined_array, which is then use by the gentable function to append a table of images. I have tried async and wait thinking it would be as simple as adding async and wait in both function. But that didn't work at all.

Any help would be greatly appreciated. I have gotten this far by reading a lot of stackoverflow questions and modifying. But I can not seems to get the last bit to work.

var combined_arrays = [];

function update() {
  var selected_csv = ['https://raw.githubusercontent.com/letsnotmakejunk/flashcards/main/Adjectives%201%20(State)%20(with%20subtitles).csv', 'https://raw.githubusercontent.com/letsnotmakejunk/flashcards/main/Adjectives%202%20(Condition)%20(with%20subtitles).csv'];
  //parse each entry of array and gen cards
  for (let l = 0, n = selected_csv.length; l < n; l++) {
    Papa.parse(selected_csv[l], {
      download: true,
      delimiter: ",",
      skipEmptyLines: true,
      header: true,
      complete: function(result) {
        //let selected_data = result.data;
        combined_arrays.push(...result.data);
      }
    });
  };
};


function gentable() {
  var comb = combined_arrays;
  var columns = 6,
    table = document.createElement('table'),
    tbody = table.appendChild(document.createElement('tbody')),
    tr, td, img;
  let display = document.getElementById("cards");
  for (i = 0, l = comb.length; i < l; i++) {
    if (i % columns == 0) tr = tbody.appendChild(document.createElement('tr'));
    let card = document.createElement("div");
    card.setAttribute('class', 'card');
    let qimages = document.createElement('img');
    qimages.setAttribute('class', 'qimages');
    qimages.setAttribute('src', '../.assets/' + comb[i].value);
    tr.appendChild(document.createElement('td'))
      .appendChild(card)
      .appendChild(qimages)
    //.src = '../.assets/' + combined_arrays[i].value;
  }
  display.appendChild(table).setAttribute('class', 'card_table');
};
<script src="https://unpkg.com/papaparse@5.3.2/papaparse.min.js"></script>
<div id='cards'>

</div>
<button onclick='update()'>
testing
</button>
<button onclick='gentable()'>
gentable
</button>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I hope this will be helpful, do you want to achieve this? So that the function update calls gentable. Let me know!

var combined_arrays = [];

function update() {
  var selected_csv = ['https://raw.githubusercontent.com/letsnotmakejunk/flashcards/main/Adjectives%201%20(State)%20(with%20subtitles).csv', 'https://raw.githubusercontent.com/letsnotmakejunk/flashcards/main/Adjectives%202%20(Condition)%20(with%20subtitles).csv'];
  //parse each entry of array and gen cards
  for (let l = 0, n = selected_csv.length; l < n; l++) {
    Papa.parse(selected_csv[l], {
      download: true,
      delimiter: ",",
      skipEmptyLines: true,
      header: true,
      complete: function(result) {
        //let selected_data = result.data;
        combined_arrays.push(...result.data);
        if (l + 1 === selected_csv.length) {
          gentable();
        }
      }
    });
  };
};


function gentable() {
  var comb = combined_arrays;
  var columns = 6,
    table = document.createElement('table'),
    tbody = table.appendChild(document.createElement('tbody')),
    tr, td, img;
  let display = document.getElementById("cards");
  for (i = 0, l = comb.length; i < l; i++) {
    if (i % columns == 0) tr = tbody.appendChild(document.createElement('tr'));
    let card = document.createElement("div");
    card.setAttribute('class', 'card');
    let qimages = document.createElement('img');
    qimages.setAttribute('class', 'qimages');
    qimages.setAttribute('src', '../.assets/' + comb[i].value);
    tr.appendChild(document.createElement('td'))
      .appendChild(card)
      .appendChild(qimages)
    //.src = '../.assets/' + combined_arrays[i].value;
  }
  display.appendChild(table).setAttribute('class', 'card_table');
};
<script src="https://unpkg.com/papaparse@5.3.2/papaparse.min.js"></script>
<div id='cards'>

</div>
<button onclick='update()'>
2 in 1
</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!