【发布时间】:2015-12-14 20:17:32
【问题描述】:
在内部循环的最后一次迭代中,我有两个嵌套的每个循环,我想调用一个包含 ajax 调用的 jquery 函数。只有在从这个 ajax 调用中获得结果之后,每个外部的下一次迭代才会发生。有什么办法可以同步这些循环。
$.ajax({
type: "POST",
contentType: 'application/json;charset=utf-8',
dataType:'json',
url: 'getCall1',
data: JSON.stringify(send_data),
success: function(json)
{
$.each(json,function(i,item){
$.ajax({
type: "POST",
contentType: 'application/json;charset=utf-8',
dataType:'json',
url: 'getcall2',
data: JSON.stringify(send_data),
success: function(json)
{
$.each(json,function(i,item){
//For last iteration of this loop i want to call a function contain another ajax call. After completion of this call i want to continue with next iteration of outer loop
callFun();
});
}
});
});
}
});
function callFun(){
$.ajax({
type: "POST",
contentType: 'application/json;charset=utf-8',
dataType:'json',
url: 'getcall3',
data: JSON.stringify(send_data),
success: function(json)
{
}
});
}
【问题讨论】:
-
您应该了解它将如何影响您的表现。几个嵌套的 AJAX 调用需要很长时间才能执行。
-
我能够尽快得到结果,但问题是两个循环没有等待函数调用'callFun'
-
我认为人们错过了你只在代码 cmets 中指定的隐含问题:
For last iteration of this loop i want to call a function contain another ajax call. After completion of this call i want to continue with next iteration of outer loop