完成所有AJAX请求后的jQuery调用函数 [英] JQuery call function when all AJAX requests are complete
问题描述
我的问题是此处的问题的变体
My question is a variant of the question here
但是,有两个区别:
-
我不知道有多少个AJAX请求.可以肯定的是,至少有10个.它们可以同时或几乎同时运行,并且请求数每次都会更改.
I don't know how many AJAX requests there are. For sure, there are at least 10. They run concurrently, or almost concurrently, and the number of requests changes every time.
我要调用的函数也会发出AJAX请求.我不希望AJAX请求永远持续下去(这是我第一次尝试时发生的事情).我只想一次使用此功能.
The function that I want to call makes AJAX requests as well. I don't want the AJAX requests to go on forever (which is what happened when I first tried). I only want this functionality one time.
这是我的代码:
$("#calculateButton").ajaxComplete(function() {
$(this).unbind();
$(this).click();
});
所以我尝试要做的是将ajaxComplete回调附加到calculate按钮.问题在于,每次ajax请求完成时都会执行此操作.而我想要的是在所有 AJAX请求完成后执行的功能.
So what I tried to do is attach the ajaxComplete callback to the calculate button. The problem is that this executes every time an ajax request finishes. Whereas what I want is for the function to execute when all AJAX requests are complete.
有没有办法做到这一点.当我写出来的时候,听起来很混乱.我应该放弃并创建一个骇人的解决方法吗?
Is there any way to do this. When I write it out, it sounds convoluted. Should I just give up and create a hacky work-around?
推荐答案
使用.ajaxStop()
代替当前方法.
$("#calculateButton").ajaxStop(function() {
$(this).unbind(); // $(this).unbind("ajaxStop") -> this is not needed as unbind will remove everything, but leaving it here as an FYI
$(this).click();
});
http://api.jquery.com/ajaxStop/
注意::更新了上面的代码以停止无限循环.
Note: Updated code above to stop infinite loop.
这篇关于完成所有AJAX请求后的jQuery调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!