JQuery ajaxStop trigger event on aborted calls

I used jQuery ajaxSend and ajaxStop to display a counter on every active ajax request. It works, unless some of my plugins interrupt their ajax requests, ajaxStop does not start and does not start until the page is refreshed. It seems that the aborted request is still considered jQuery. Can I create an ajaxStop trigger or is there a better way?

+3
source share
3 answers

Well, I figured out a way that I really like. Correct the XMLHttpRequest undo method so that it decreases the jQuery ajax counter and starts the handler if it has the last request. Not sure how well it will work in browsers.

var old_abort = XMLHttpRequest.prototype.abort;
XMLHttpRequest.prototype.abort = function(){
    jQuery.proxy(old_abort, this)();
    if ( jQuery.active-- === 1 )
        jQuery.event.trigger( "ajaxStop" );
};
+3
source

This is apparently the only problem 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

Go to jQuery 1.5.1 or apply the patch posted by Jake above. In addition, I had to modify the patch as follows before it worked correctly for my use cases:

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--;
  }
};
+2
source

complete - , :

complete (Local event)
This event, regardless of whether the request was successful or not. You will always receive a full callback, even for synchronous requests.

$.ajax({
   complete: function(){
     // hide your spinner
   }
 });

Not sure if you need this, but you can raise an ajaxStop event. $(this).trigger('ajaxStop');

+1
source

Source: https://habr.com/ru/post/1752025/