[英]How to access stored data from d3 async calls in document.ready function to pass it to a function
I was trying to store data from a async call to a variable/object/array outside the function.我试图将数据从异步调用存储到函数外部的变量/对象/数组。 I have seen couple of solutions to make calls synchronous, using callbacks etc but none of them works for my scenario.
我已经看到了几种使调用同步、使用回调等的解决方案,但它们都不适用于我的场景。 This is what I am trying to do
这就是我想要做的
var x = [];
d3.json(url, response){
if(response{
setInterval(funtion (){
d3.json(url, response){
x=response;
});
},5000);
}
}
$(document).ready(function(){
console.log(x);
$.each(x, function(i,val){
function(val);
});
});
You need to make deferred jquery function calls something like this:您需要使延迟的 jquery 函数调用如下:
function test(){
var defer = $.Deferred();
setTimeout(function(){console.log("prints 1st");defer.resolve();},1000);
return defer;
}
function test2(){
console.log("prints 2nd");
return $.when();
}
test().then(test2).then(function () {
console.log("prints at the end.");
});
Here is a working example http://jsbin.com/payitucami/edit?html,console这是一个工作示例http://jsbin.com/payitucami/edit?html,console
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.