最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - what is the maximum timeout can be set in jquery ajax? - Stack Overflow

programmeradmin6浏览0评论
$.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?

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 .

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

$.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?

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 .

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

Share Improve this question asked May 7, 2014 at 6:04 Jianxun LianJianxun Lian 1371 gold badge2 silver badges5 bronze badges 4
  • 4 Why would you do an ajax request that will be answered in 1.5 hours ? – adeneo Commented May 7, 2014 at 6:07
  • 1.5h is the worst expected case. Most other ajax will return in seconds. – Jianxun Lian Commented May 7, 2014 at 6:12
  • You have to think about UX. – Satpal Commented May 7, 2014 at 6:12
  • 2 Depending on your server-side language -- start a background process or thread, then have your JS ask 'are you down' -- at first, maybe every 15 seconds, then every minute, then every five, then every 15... – Jeremy J Starcher Commented May 7, 2014 at 7:31
Add a ment  | 

2 Answers 2

Reset to default 6

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");
        }
    }

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

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

It came back with

9393076 
9393081 
9393081 

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

Which equals to ~156 minutes.

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).

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

发布评论

评论列表(0)

  1. 暂无评论