1

我可能把这一切都倒退了,而且恐怕我已经很久没有做过太多的 javascript 了,从那时起事情已经发生了很大的变化。因此,答案可能非常微不足道,但我无法在网上找到任何有用的东西。

很简单,我想在本地 (/log.csv) 或远程 ( http://mywebsite.com/log.csv ) 解析一个 csv 文件,我希望能够在脚本的其余部分。换句话说,理想情况下是这样的:

var mydata = Papa.parse("http://fetconsulting.co.uk/demo-fleetdrive/log_full.csv", {
   download: true,
   complete: function(results) {
       console.log(results);
   }
});

alert(mydata.data.length);
myfantasticplottingfunction(mydata);

想法?

万分感谢!

4

1 回答 1

1

这解决了我的问题:

Papa.parse("http://mywebsite.com/log.csv", {
    download: true,
    complete: function(results) {

        (function myfantasticplottingfunction(container) {

         // Do amazing things with envision.js on results
         console.log(results);
         alert(results.length);

         return new envision.templates.TimeSeries(options);
        })(document.getElementById("editor-render-0"));
     }
});

基本上,我确实确实让事情落后并误解了回调函数。变量 results 超出范围,因为异步产生,因此很难/不可能返回“通常的方式”。简单的解决方法是将我的可视化放在回调的范围内。

谢谢!

于 2015-09-24T19:38:33.093 回答