Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 261
Jianxun Lian Avatar asked May 07 '14 06:05

Jianxun Lian


People also ask

What is the default timeout for jQuery AJAX?

The default value is 0 , which means there is no timeout.

Does AJAX have a timeout?

Session timeout has been a very common feature in Ajax-based web applications. In responsive interface, the programmer needs to delay the ajax request to achieve some task before the response. This can be achieved by using jQuery setTimeout() function.

Is there a way to limit the time in AJAX call will run?

The timeout period starts at the point the $. ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent.

How do you call AJAX every 5 seconds?

Using setInterval() It repeatedly calls the function on the given interval for stop you need to clear the interval using clearInterval() or close the window. Creating a function that calls the AJAX request and using this function in setInterval() and set Interval for 5 sec.


2 Answers

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

like image 171
Matas Vaitkevicius Avatar answered Sep 29 '22 19:09

Matas Vaitkevicius


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

like image 42
Sebastian Xawery Wiśniowiecki Avatar answered Sep 29 '22 20:09

Sebastian Xawery Wiśniowiecki