Skip to content

Instantly share code, notes, and snippets.

@tegomass
Forked from Integralist/cancelAJAX.js
Last active April 20, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tegomass/5df891403b4a90d4aa036aba93c75916 to your computer and use it in GitHub Desktop.
Save tegomass/5df891403b4a90d4aa036aba93c75916 to your computer and use it in GitHub Desktop.
Cancel all jQuery AJAX requests currently running
let xhrQueue = [];
$(document).ajaxSend(function(event,jqxhr,settings){
xhrQueue.push(jqxhr);
});
$(document).ajaxComplete(function(event,jqxhr,settings){
let i;
if((i=$.inArray(jqxhr,xhrQueue)) > -1){
xhrQueue.splice(i,1);
}
});
function ajaxAbort(){
let i=0;
while(xhrQueue.length){
xhrQueue[i++].abort();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment