Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Automatically abort AjaxRequests on Page Unload?

Tags:

jquery

ajax

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?

like image 618
Eli Avatar asked Mar 01 '09 20:03

Eli


People also ask

How do I abort AJAX request?

Just use ajax.abort(); } //then you make another ajax request $. ajax( //your code here );

How do I fire AJAX request periodically?

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.

How do I make jQuery wait for an AJAX call to finish before it returns?

If you don't want the $. ajax() function to return immediately, set the async option to false : $(". my_link").

Does AJAX have a timeout?

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.


2 Answers

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);
};
like image 145
yourpublicdisplayname Avatar answered Oct 13 '22 16:10

yourpublicdisplayname


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

like image 41
Madman Avatar answered Oct 13 '22 16:10

Madman