我的问题是这里问题的一个变种
但是,有两点不同:
我不知道有多少个AJAX请求。 当然,至少有10个。它们同时运行或几乎同时运行,并且每次请求的数量都会发生变化。
我想调用的函数也会产生AJAX请求。 我不希望AJAX请求永远持续下去(这是我第一次尝试时发生的情况)。 我只想要这个功能一次。
这是我的代码:
$("#calculateButton").ajaxComplete(function() { $(this).unbind(); $(this).click(); });所以我试图做的是将ajaxComplete回调附加到calculate按钮。 问题是每次ajax请求结束时都会执行此操作。 而我想要的是当所有的 AJAX请求都完成时,函数执行。
有没有办法做到这一点。 当我写出来时,听起来很复杂。 我应该放弃,并创建一个hacky变通?
My question is a variant of the question here
However, there are two differences:
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.
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.
Here is my code:
$("#calculateButton").ajaxComplete(function() { $(this).unbind(); $(this).click(); });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/
注意:更新上面的代码以停止无限循环。
Use .ajaxStop() instead of your current method.
$("#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.
更多推荐
发布评论