浏览器的 AJAX (XmlHttpRequest) 超时长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9502410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AJAX (XmlHttpRequest) timeout length by browser
提问by wajiw
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?
我一直在网上搜索,试图找到一个直接的答案。有谁知道浏览器对ajax请求的默认超时长度?如果更改了,也是按版本吗?
采纳答案by Liviu
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)
我认为浏览器没有 AJAX 超时,只有同步或异步请求;同步 - 首先冻结 JavaScript 执行,直到请求返回,异步 - 不会冻结 JavaScript 执行,它只是将请求从执行流中取出,如果您有回调函数,它将与正在运行的脚本并行执行该函数(类似于线程)
**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
回答by monsur
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.
根据规范,超时值默认为零,这意味着没有超时。但是,您可以在 XHR.timeout 属性上设置超时值;该值以毫秒为单位。
Sources:
资料来源:
http://www.w3.org/TR/2011/WD-XMLHttpRequest2-20110816/#the-timeout-attributehttp://msdn.microsoft.com/en-us/library/cc304105(v=vs.85).aspx
http://www.w3.org/TR/2011/WD-XMLHttpRequest2-20110816/#the-timeout-attribute http://msdn.microsoft.com/en-us/library/cc304105(v=vs.85) .aspx
回答by John
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 timeoutto something low like 1000ms until I could ensure I had minimal code (you mustput the xhr.timeoutafter openand beforesend).
我做了少量的测试。为了测试我加载了我的网站,停止了本地服务器,然后尝试了一个 AJAX 请求。我将 设置为timeout1000ms 之类的低,直到我可以确保我有最少的代码(你必须把xhr.timeoutafteropen和beforesend)。
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 timeoutwould be outright ignored by browsers. My goal reformed in to trying to determine what the maximum timeout couldbe 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.
一旦我开始工作,我最初的目标是确定允许的适当时间量,但是我很惊讶timeout浏览器会完全忽略它的速度。我的目标是在错误处理不再可行之前尝试确定最大超时可能是多少。这意味着经过这些相当短的时间跨度,您的超时处理程序脚本将根本无法工作。我发现的东西非常可悲。
- 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.
- Chrome 60: 995ms, 996ms 会向控制台抛出一个肮脏的邪恶错误。
- Firefox 52 ESR:~3000 毫秒,鼠标位置或其他问题可能会导致大约或不到三秒没有响应。
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 timeoutis set higher than 995ms Chromewill ignore your code and puke on your nice clean empty console that you worked hard to keep clean. Firefoxis not much better and there are unreliable requests that just timeout for well beyond any patience I have and in doing so ignore the ontimeouthandler.
因此,如果您timeout的设置高于 995 毫秒,Chrome将忽略您的代码,并在您努力保持干净的漂亮干净的空控制台上呕吐。Firefox也好不到哪里去,并且有一些不可靠的请求会超时,超出我的耐心,因此忽略ontimeout处理程序。
回答by Satyam Naolekar
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
浏览器确实有超时值,行为取决于浏览器 chrome 的超时值为 5 分钟,5 分钟后它会重新发送 ajax 调用

