Skip to content

Instantly share code, notes, and snippets.

@msankhala
Created May 25, 2016 08:07
Show Gist options
  • Save msankhala/3fa2844c1fbad1f4c0185a8e3ef09aed to your computer and use it in GitHub Desktop.
Save msankhala/3fa2844c1fbad1f4c0185a8e3ef09aed to your computer and use it in GitHub Desktop.
Stop all ajax request
// Stop all ajax request by http://tjrus.com/blog/stop-all-active-ajax-requests
$.xhrPool = []; // array of uncompleted requests
$.xhrPool.abortAll = function() { // our abort function
$(this).each(function(idx, jqXHR) {
jqXHR.abort();
});
$.xhrPool.length = 0
};
$.ajaxSetup({
beforeSend: function(jqXHR) { // before jQuery send the request we will push it to our array
$.xhrPool.push(jqXHR);
},
complete: function(jqXHR) { // when some of the requests completed it will splice from the array
var index = $.xhrPool.indexOf(jqXHR);
if (index > -1) {
$.xhrPool.splice(index, 1);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment