what is the maximum timeout can be set in jquery a

2019-04-28 20:58发布

问题:

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

回答1:

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

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



回答2:

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