我有一个不属于我的 util 类。我正在调用该 util 类的方法之一,该方法又调用一个 ajax 函数。并给我回复。
我需要根据响应对象做出决定。问题是响应对象需要一些时间来填充(毫秒)。
var selector = dojo.byId("SelectorId");
var theChart = new chart( selector, 135, 92, style, frequency, time);
if(theChart.data ===null){
console.log("No response");
}else{
Console.log("Use response data");
}
和
chart( selector, 135, 92, style, frequency, time);
不属于我所有。 Chart() 来自 util 类。
上面的代码片段可以很好地使用断点。但是当我删除断点时,它总是开始进入“if” block 。
如何解决这个问题。
最佳答案
如果您使用setTimeout,它应该可以解决您的问题:
var selector = dojo.byId("SelectorId");
var theChart = new chart( selector, 135, 92, style, frequency, time);
setTimeout(function(){
if(theChart.data ===null){
console.log("No response");
}else{
Console.log("Use response data");
}
}, 100);
或者,您可以使用setInterval。下面的示例检查变量 avery 100msecs,并在 N=10 次尝试后返回“false”:
var maxTries = 10;
var checkFunction = function(){
if(theChart.data === null){
--maxTries;
}else{
Console.log("Use response data");
clearTimeout(checkFunction);
return;
}
if( 0 == maxTries ) {
console.log("No response");
clearTimeout(checkFunction);
}
} ;
setTimeout(checkFunction, 100);
关于javascript - Ajax 响应需要时间来填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4546674/