如何将d3.csv.parse的结果保存到全局变量? [英] How to save the result of d3.csv.parse to a global variable?

查看:442
本文介绍了如何将d3.csv.parse的结果保存到全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个

var result;
d3.csv("xxx.csv",function(data){
        csvResultParser(data);
});

function csvResultParser(data){
  //parse the data then assign it to result  
}

但我仍然有结果为未定义,任何线索?

But I still have result as "undefined", any clues?

推荐答案

d3.csv()函数是异步的。因此,在读取 result 变量之前,必须等待接收到数据。这就是为什么在处理异步数据时,优先使用 d3.csv()函数而不是使用全局变量。

The d3.csv() function is asynchronous. Thus, you have to wait for the data to be received before reading the result variable. This is the reason why, when dealing with asynchronous data, it is prefered to do everything inside the d3.csv() function instead of using global variables.

这篇关于如何将d3.csv.parse的结果保存到全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆