AJAX (XmlHttpRequest) timeout length by browser

前端 未结 4 2028
庸人自扰
庸人自扰 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.

提交回复
热议问题