Do I need to interrupt the AJAX timeout?

I have an ajax GET request with a 2 second timeout. I do not want the request to still hang there if the request expires. After 2 seconds, I just want to stop everything.

I am wondering if abort() needs to be called if the timeout is reached, or reaches 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 
+6
source share
1 answer

No, this is not necessary. Inside jQuery there will be an abort XHR for you when the timeout is removed.

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); } 
+4
source

Source: https://habr.com/ru/post/973609/


All Articles