可能重复:
Multiple ajax calls inside a each() function.. then do something once ALL of them are finished?
基本上,我想在每个循环完成后,包含load()的each()循环后运行一个函数。加载每个链接需要一些时间,并且正在等待浏览器赶上,但脚本已经进入下一步。有什么想法吗?
jQuery('#theId a').each(function() { // contains many links
toLoad = jQuery(this).attr('href');
jQuery('#containment').load(toLoad, function(response) {
//do stuff
});
});
//... Do stuff after the above is really complete.
答案 0 :(得分:5)
尝试这样的事情:
var e = jQuery("#theId a"), l = e.length;
e.each(function() {
toLoad = this.attr("href"); // I'm fairly sure "this" is already a jQuery object
jQuery("#containment").load(toLoad, function(response) {
// do stuff
l--;
if( l == 0) {
// this is the last one - do ending stuff here.
}
});
});
如果“this”还不是jQuery对象,只需将该行还原为您拥有的那个。