I've used jQuery's ajaxSend and ajaxStop to show a spinner whenever ajax requests are active. It works except when some of my plugins abort their ajax requests, ajaxStop does not trigger and will not trigger until after the page is refreshed. It seems like the aborted request still counts to jQuery. Can I make ajaxStop trigger or is there a better way?
我已经使用jQuery的ajaxSend和ajaxStop来显示每当ajax请求处于活动状态时的微调器。它的工作原理除非我的一些插件中止了他们的ajax请求,ajaxStop不会触发,直到页面刷新后才会触发。似乎中止的请求仍然依赖于jQuery。我可以制作ajaxStop触发器还是有更好的方法?
3 个解决方案
#1
3
Well I figured out a way that I quite like. Patch the XMLHttpRequest abort method so that it will decrease jQuery's ajax counter and trigger the handler if it's the last request. Not sure yet how well it will work across browsers.
好吧,我想出了一个我非常喜欢的方式。修补XMLHttpRequest中止方法,以便减少jQuery的ajax计数器并触发处理程序(如果它是最后一个请求)。还不确定它在各种浏览器中的效果如何。
var old_abort = XMLHttpRequest.prototype.abort;
XMLHttpRequest.prototype.abort = function(){
jQuery.proxy(old_abort, this)();
if ( jQuery.active-- === 1 )
jQuery.event.trigger( "ajaxStop" );
};
#2
2
This seems to only be an issue in jQuery 1.4.2 for dataType = "json", see http://forum.jquery.com/topic/ajaxstart-not-fired-when-jquery-active-changes-from-0-to-1
这似乎只是jQuery 1.4.2中dataType =“json”的问题,请参阅http://forum.jquery.com/topic/ajaxstart-not-fired-when-jquery-active-changes-from-0-到1
Upgrade to jQuery 1.5.1 or apply the patch posted by Jake above. Also, I had to modify the patch as follows before it would work correctly for my use cases:
升级到jQuery 1.5.1或应用上面Jake发布的补丁。此外,我必须按照以下方式修改补丁,然后才能正常使用我的用例:
f_abort = XMLHttpRequest.prototype.abort;
XMLHttpRequest.prototype.abort = function() {
$.proxy(f_abort, this)();
if ($.active === 1) {
$.event.trigger("ajaxStop");
$.active = 0;
} else if ($.active > 1) {
$.active--;
}
};
#3
1
I would use the complete event instead - that runs no matter what:
我会使用完整的事件 - 无论如何运行:
complete (Local Event)
This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests.complete(本地事件)无论请求是否成功,都会调用此事件。即使是同步请求,您也将始终收到完整的回调。
$.ajax({
complete: function(){
// hide your spinner
}
});
not sure if you need this, but you can trigger the ajaxStop event. $(this).trigger('ajaxStop');
不确定你是否需要这个,但你可以触发ajaxStop事件。 $(本).trigger( 'ajaxStop');
#1
3
Well I figured out a way that I quite like. Patch the XMLHttpRequest abort method so that it will decrease jQuery's ajax counter and trigger the handler if it's the last request. Not sure yet how well it will work across browsers.
好吧,我想出了一个我非常喜欢的方式。修补XMLHttpRequest中止方法,以便减少jQuery的ajax计数器并触发处理程序(如果它是最后一个请求)。还不确定它在各种浏览器中的效果如何。
var old_abort = XMLHttpRequest.prototype.abort;
XMLHttpRequest.prototype.abort = function(){
jQuery.proxy(old_abort, this)();
if ( jQuery.active-- === 1 )
jQuery.event.trigger( "ajaxStop" );
};
#2
2
This seems to only be an issue in jQuery 1.4.2 for dataType = "json", see http://forum.jquery.com/topic/ajaxstart-not-fired-when-jquery-active-changes-from-0-to-1
这似乎只是jQuery 1.4.2中dataType =“json”的问题,请参阅http://forum.jquery.com/topic/ajaxstart-not-fired-when-jquery-active-changes-from-0-到1
Upgrade to jQuery 1.5.1 or apply the patch posted by Jake above. Also, I had to modify the patch as follows before it would work correctly for my use cases:
升级到jQuery 1.5.1或应用上面Jake发布的补丁。此外,我必须按照以下方式修改补丁,然后才能正常使用我的用例:
f_abort = XMLHttpRequest.prototype.abort;
XMLHttpRequest.prototype.abort = function() {
$.proxy(f_abort, this)();
if ($.active === 1) {
$.event.trigger("ajaxStop");
$.active = 0;
} else if ($.active > 1) {
$.active--;
}
};
#3
1
I would use the complete event instead - that runs no matter what:
我会使用完整的事件 - 无论如何运行:
complete (Local Event)
This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests.complete(本地事件)无论请求是否成功,都会调用此事件。即使是同步请求,您也将始终收到完整的回调。
$.ajax({
complete: function(){
// hide your spinner
}
});
not sure if you need this, but you can trigger the ajaxStop event. $(this).trigger('ajaxStop');
不确定你是否需要这个,但你可以触发ajaxStop事件。 $(本).trigger( 'ajaxStop');