AJAX (XmlHttpRequest) timeout length by browser

前端 未结 4 2029
庸人自扰
庸人自扰 2020-12-30 02:35

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 chan

相关标签:
4条回答
  • 2020-12-30 03:08

    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.

    0 讨论(0)
  • 2020-12-30 03:09

    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

    0 讨论(0)
  • 2020-12-30 03:16

    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
    
    0 讨论(0)
  • 2020-12-30 03:18

    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

    0 讨论(0)
提交回复
热议问题