Skip to content

Instantly share code, notes, and snippets.

@DmitriyWebDev
Forked from msankhala/stop-all-ajax-requset.js
Created September 28, 2020 17:31
Show Gist options
  • Save DmitriyWebDev/b65bfa10d09230bf6ab5b6cd7cb4400c to your computer and use it in GitHub Desktop.
Save DmitriyWebDev/b65bfa10d09230bf6ab5b6cd7cb4400c 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