javascript jquery ajax中可以设置的最大超时时间是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23509926/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:01:47  来源:igfitidea点击:

what is the maximum timeout can be set in jquery ajax?

javascriptjqueryajaxtimeouthttp-status-code-404

提问by Jianxun Lian

$.ajax(
   url:"",
   async: true,
   timeout: 2*60*60*1000, //2 hours,
   success: function(){},
   error:  function(){}
);

In a jQuery ajax request, if I set the timeout with a big value, or left it empty, will it keep waiting until the server returns a result?

在 jQuery ajax 请求中,如果我将超时设置为大值,或者将其留空,它会一直等待直到服务器返回结果吗?

Actually, I expect the server will response in 1.5 hours, so in my js script I set timeout to 2 hours, but I found the Ajax jump to error function (with msg code 404) in less than 1 hour. It means ajax abort the waiting ahead of the time .

实际上,我预计服务器会在 1.5 小时内响应,所以在我的 js 脚本中我将超时设置为 2 小时,但我发现 Ajax 跳转到错误函数(带有 msg 代码 404)不到 1 小时。这意味着 ajax 提前中止等待。

So I wonder if there is a maximum timeout value can ajax be set?

所以我想知道ajax是否可以设置最大超时值?

回答by Matas Vaitkevicius

My previous answer was wrong (timeout just seemed to be to short and I couldn't believe it myself) so I have done a test yesterday, created 1GB zip then throttled my connection with fiddler and wrote this aspx page.

我之前的答案是错误的(超时似乎很短,我自己都不敢相信)所以我昨天做了一个测试,创建了 1GB zip,然后限制了我与 fiddler 的连接并编写了这个 aspx 页面。

public partial class Ajaxtest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            Response.BufferOutput = false; 
            Response.WriteFile("c://OnTheMoveOffline.zip");
        }
    }

then I ran this script (interestingly fiddler blew up with OutOfMemory exception within 10 seconds however response was held).

然后我运行了这个脚本(有趣的是,fiddler 在 10 秒内因 OutOfMemory 异常而爆炸,但响应被保留)。

var f = function(){ 
var compareDate = new Date();
$.ajax({
 url : 'http://localhost:22037/FeatureDev/Ajaxtest.aspx',
success: function(data){ console.log(Math.abs(new Date() - compareDate));},
error : function(e){ console.log(Math.abs(new Date() - compareDate));},
timeout : 10000000
}).done(function() {
    console.log(Math.abs(new Date() - compareDate));
  })
  .fail(function() {
    console.log(Math.abs(new Date() - compareDate));
  })
  .always(function() {
    console.log(Math.abs(new Date() - compareDate));
  });}

It came back with

它回来了

9393076 
9393081 
9393081 

9393076/1000 ~ 9393(s) = 02:36:33

9393076/1000 ~ 9393(s) = 02:36:33

Which equals to ~156 minutes.

这等于 ~156 分钟。

I will repeat this test this weekend to see if it will timeout after same amount of time, but so far it seems it is more than 7200000 (2*60*60*1000).

我将在本周末重复此测试,看看它是否会在相同的时间后超时,但到目前为止似乎超过 7200000 (2*60*60*1000)。

回答by Sebastian Xawery Wi?niowiecki

Default global value of timeoutis 0 that means it is infinite.

的默认全局值为timeout0,表示它是无限的。