Thanks for reading.
I've noticed that if I have a page that has one or more ajax requests open, and I click a link to leave a page, or refresh it, it will wait for the ajax requests to complete before unloading.
This isn't usually an issue, but if the request takes a while, it can be.
I'm looking for something like:
$(window).bind("beforeunload", function() {AjaxRequest.abort();});
to automatically abort requests before unload, but not quite sure how to find the ajax requests. Would they be under window somewhere?
Just use ajax.abort(); } //then you make another ajax request $. ajax( //your code here );
Use setInterval() when you want to send AJAX request at a particular interval every time and don't want to depend on the previous request is completed or not. But if you want to execute the AJAX when the previous one is completed then use the setTimeout() function.
If you don't want the $. ajax() function to return immediately, set the async option to false : $(". my_link").
JIRA has a default AJAX timeout period so that requests in the browser do not run for unlimited amounts of time. Normally 30 seconds is plenty of time for a typical data request. AJAX timeouts can be caused by many different things.
The following code is tested and aborts all outstanding jQuery ajax requests on page unload in Chrome, but it gets used by Edge and IE using clients as well without error.
Put the following as the first jQuery ready
handler:
$(onPageLoaded)
And put this somewhere accessible:
function onPageLoaded() {
var jqxhrs = [];
$(window).bind("beforeunload", function (event) {
$.each(jqxhrs, function (idx, jqxhr) {
if (jqxhr)
jqxhr.abort();
});
});
function registerJqxhr(event, jqxhr, settings) {
jqxhrs.push(jqxhr);
}
function unregisterJqxhr(event, jqxhr, settings) {
var idx = $.inArray(jqxhr, jqxhrs);
jqxhrs.splice(idx, 1);
}
$(document).ajaxSend(registerJqxhr);
$(document).ajaxComplete(unregisterJqxhr);
};
You have to persist all you ajax request and when page is being reloading abort all requests. Sample
var _requests = [];
var _abortAllRequests = function () {
$(_requests).each(function (i, xhr) { xhr.abort(); });
_requests = [];
}
$(window).on("beforeunload", function () {
_abortAllRequests();
});
somewhere in your code
_requests.push($.ajax(...))
Additionally you can pop done requests using $.ajaxSetup with events ajaxStart ajaxStop, but its up to you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With