Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it necessary to abort on AJAX timeout?

Tags:

jquery

I have an ajax GET request with a 2 second timeout. I don't want the request to still be hanging out there if the request times out. At 2 seconds, I just want to stop everything.

I'm wondering if it is necessary to call abort() if timeout has been reached, or does reaching the timeout threshold automatically abort everything...?

request = $.ajax({
            type: 'GET',
            url: url,
            timeout: 2000, 
            success: function (data) {

                // do some stuff

            },
            error: function(x, t, m) {

                if(t==="timeout") {
                    request.abort(); // is this necessary?
                    // do some other stuff

                } // end if timeout
            } // end error function
        }); // end ajax 
like image 862
php die Avatar asked Aug 10 '14 16:08

php die


1 Answers

No, it is not necessary. Internally, jQuery will abort the XHR for you when the timeout is hit.

If you check the source of $.ajax you can see this in action:

// Timeout
if (s.async && s.timeout > 0) {
    timeoutTimer = setTimeout(function () {
        jqXHR.abort("timeout");
    },
    s.timeout);
}
like image 162
Rory McCrossan Avatar answered Sep 20 '22 03:09

Rory McCrossan