Skip to content

Instantly share code, notes, and snippets.

@gagimilicevic
Forked from msankhala/stop-all-ajax-requset.js
Created December 28, 2020 16:26
Show Gist options
  • Save gagimilicevic/acf160c99d018ded1897e5df721891e0 to your computer and use it in GitHub Desktop.
Save gagimilicevic/acf160c99d018ded1897e5df721891e0 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