In my javascript file for a page, I would like to execute some scripts after all the XHR have returned. I know that there is ajaxStop()
for XHR requests through jquery, but since I am using Mithril and it has its own wrapper around Ajax, I can't really use ajaxStop()
.
I have also tried some other solutions like
function isAllXhrComplete(){
window.activeAjaxCount--;
if (window.activeAjaxCount === 0){
//execute something here.
}
}
(function(open) {
XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {
this.addEventListener("load", isAllXhrComplete());
open.call(this, method, url, async, user, pass);
};
})(XMLHttpRequest.prototype.open);
(function(send) {
XMLHttpRequest.prototype.send = function (data) {
window.activeAjaxCount++;
send.call(this, data);
};
})(XMLHttpRequest.prototype.send);
But it seems that since all the XHR calls are fired separately, the window.activeAjaxCount
would always be 1 or 0, thus triggering the execution of script on every finished XHR request.
Is there any good ways to wait for all XHR requests to return?