Skip to content

Instantly share code, notes, and snippets.

@Fordi
Created June 30, 2011 16:03
Show Gist options
  • Save Fordi/1056544 to your computer and use it in GitHub Desktop.
Save Fordi/1056544 to your computer and use it in GitHub Desktop.
Function for killing all running ajax requests in jQuery
(function ($) {
var _ajax = $.ajax,
xhrQueue = [];
$.ajax = function () {
var ret;
xhrQueue.push(ret=_ajax.apply(this, arguments));
return ret;
};
$.ajaxCancel = function () {
while (xhrQueue.length>0) xhrQueue.pop().abort();
};
})(jQuery);
@trevnorris
Copy link

(function ($) {
    var xhrQueue = [];
    $.ajaxSetup({ beforeSend: function () {
        xhrQueue.push( this );
    }});
    $.ajaxCancel = function () {
        while ( xhrQueue.length > 0) xhrQueue.pop().abort();
    };
})(jQuery);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment