假设我有以下内容:
function main() {
var finished = false;
for(var i=0; i < 3; i++) {
do(i);
}
}
function do(i) {
$.ajax({
url:"myurl.com/"+i,
datatype:"text/xml",
success: function() {
// Two more layers of multiple nested $.ajax elements here
}
})
}
Run Code Online (Sandbox Code Playgroud)
在三次迭代完成所有"do"后,是否有某种方法可以弹出一个警告框?怎么样?语法会有所帮助.
Ale*_*Mcp 13
ajax调用返回一个jQuery Promise对象.您可以在数组中收集每个输出,并用于$.when将promises"捆绑"在一起.
此代码是您想要的基本理念:
function main() {
var finished = false;
var defs = [];
for(var i=0; i < 3; i++) {
defs.push(do(i));
}
$.when.apply(null, defs).done(function() {
//this code will only run when all ajax calls are complete
});
}
function do(i) {
var promise = $.ajax({
url:"myurl.com/"+i,
datatype:"text/xml",
success: function() {
// Two more layers of multiple nested $.ajax elements here
}
})
return promise;
}
Run Code Online (Sandbox Code Playgroud)
浏览器可以有很多开放的HTTP连接,不要让反对者说服你.以下是浏览器支持的最大并发连接数表.让您的网站使用情况统计数据成为您的指南,但即使是2个ajax请求也会比完全同步的数据请求更快...
Firefox 2: 2
Firefox 3+: 6
Opera 9.26: 4
Opera 12: 6
Safari 3: 4
Safari 5: 6
IE 7: 2
IE 8: 6
IE 10: 8
Chrome: 6
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2329 次 |
| 最近记录: |