可以在jquery ajax中设置最大超时时间是多少?
$.ajax(
url:"",
async: true,
timeout: 2*60*60*1000, //2 hours,
success: function(){},
error: function(){}
);
在jQuery ajax请求中,如果我将超时值设置为一个大值,或者将其保留为空,那么它将一直等待直到服务器返回结果吗?
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?
实际上,我希望服务器将在1.5小时内响应,因此在我的js脚本中将超时设置为2小时,但是我发现Ajax可以在不到1小时的时间内跳转到错误功能(使用msg代码404).这意味着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 .
所以我想知道是否可以设置ajax的最大超时值?
So I wonder if there is a maximum timeout value can ajax be set?
我以前的回答是错误的(超时似乎太短了,我自己也不敢相信) 所以昨天我做了一个测试,创建了1GB的zip,然后限制了我与小提琴手的连接,并编写了这个aspx页面.
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.
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");
}
}
然后,我运行了该脚本(有趣的是,提琴手在10秒内用OutOfMemory异常将其炸毁,但仍保持了响应).
then I ran this script (interestingly fiddler blew up with OutOfMemory exception within 10 seconds however response was held).
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));
});}
它又回来了
9393076
9393081
9393081
9393076/1000〜9393(s)= 02:36:33
9393076/1000 ~ 9393(s) = 02:36:33
大约等于156分钟.
我将在本周末重复此测试,以查看在相同的时间后是否会超时, 但到目前为止,它似乎超过了7200000(2 * 60 * 60 * 1000).
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).