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 
                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);
}
                        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