21

I've been scouring the web trying to find a straight answer to this. Does anyone know the default timeout lengths for ajax request by browser? Also by version if it's changed?

Andy
  • 8,870
  • 1
  • 31
  • 39
wajiw
  • 12,239
  • 17
  • 54
  • 73

4 Answers4

22

According to the specs, the timeout value defaults to zero, which means there is no timeout. However, you can set a timeout value on the XHR.timeout property; the value is in milliseconds.

Sources:

http://www.w3.org/TR/2011/WD-XMLHttpRequest2-20110816/#the-timeout-attribute http://msdn.microsoft.com/en-us/library/cc304105(v=vs.85).aspx

monsur
  • 45,581
  • 16
  • 101
  • 95
  • 9
    But there is an underlying timeout which is not covered by the spec. From the second link: `If you set an XMLHttpRequest time-out value that is larger than the network stack's time-out value, the network stack will time out first and the ontimeout event will not be raised.` – djjeck May 13 '14 at 18:32
5

I don't think browsers have a timeout for AJAX, there is only synchronous or asynchronous requests; synchronous - first freezes the JavaScript execution until the request returns, asynchronous - does not freeze JavaScript execution, it simply takes the request out of the execution flow, and if you have a callback function it will execute the the function in parallel with the running scripts (similar to a thread)

**sync flow:**

running JS script
     |
    ajax
(wait for response)
     |
execute callback 
     |
running JS script




 **async flow:**

 running JS script
     |
    ajax  --------------------
     |                       |
 running JS script       execute callback
Liviu
  • 452
  • 1
  • 5
  • 14
  • 2
    Chrome does seem to have a timeout. Even if I set an ajax call timeout to 900000 (15 minutes), the requests times out after exactly 1 minute with status 'canceled' on a long running request. – Jean-François Beauchamp Apr 06 '18 at 15:50
  • 1
    M87's answer on this thread also confirms this: https://stackoverflow.com/questions/42798882/is-there-any-maximum-minimum-timeout-for-the-xmlhttprequest – Jean-François Beauchamp Apr 06 '18 at 16:00
3

I did a modest amount of testing. To test I loaded my website, stopped the local server and then attempted an AJAX request. I set the timeout to something low like 1000ms until I could ensure I had minimal code (you must put the xhr.timeout after open and before send).

Once I got it working my initial goal was to determine the appropriate amount of time to allow however I was surprised how quickly the timeout would be outright ignored by browsers. My goal reformed in to trying to determine what the maximum timeout could be before error handling was no longer viable.That means past these fairly short spans of time your timeout handler script will not work at all. What I found was pretty pathetic.

  • Chrome 60: 995ms, 996ms will throw a dirty evil error in to the console.
  • Firefox 52 ESR: ~3000ms, position of mouse or other issue may cause no response around or just under three seconds.

So...

xhr.open(method,url,true);
xhr.timeout = 995;//REALLY short
xhr.send(null);
xhr.ontimeout = function ()
{
 //Code will only execute if at or below *effective* timeouts list above.
 //Good spot to make a second attempt.
}

So if your timeout is set higher than 995ms Chrome will ignore your code and puke on your nice clean empty console that you worked hard to keep clean. Firefox is not much better and there are unreliable requests that just timeout for well beyond any patience I have and in doing so ignore the ontimeout handler.

John
  • 1
  • 13
  • 98
  • 177
1

Browser does have a timeout value, behavior depends upon browser chrome has timeout value of 5 minutes and after 5 minutes it does resend ajax call

Satyam Naolekar
  • 560
  • 7
  • 10