Created
May 25, 2016 08:07
-
-
Save msankhala/3fa2844c1fbad1f4c0185a8e3ef09aed to your computer and use it in GitHub Desktop.
Stop all ajax request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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