How do I ensure D3 finishes loading several CSVs before javascript runs?

前端 未结 2 660
旧时难觅i
旧时难觅i 2021-02-01 08:01

I\'m loading a CSV of a list of other CSV files into javascript using D3.

When I run the following code, the employees array is still empty by the time it gets

相关标签:
2条回答
  • 2021-02-01 08:21

    You could use queue.js to collect the results from all the d3.csv calls:

    function parseList(filenames){
      var q = queue();
      filenames.forEach(function(d) {
        //add your csv call to the queue
        q.defer(function(callback) {
          d3.csv(d.filename,function(res) { callback(null, res) });
        });
      });
    
      q.await(restOfCode)
    }    
    
    function restOfCode(err, results) {
      //results is an array of each of your csv results
      console.log(results)
    }
    
    0 讨论(0)
  • 2021-02-01 08:25

    Your code seems fine. It's just that you're logging employees before the data is ready. But if you console.log it on the last line of parseList you should have it.

    0 讨论(0)
提交回复
热议问题