$.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?
The default value is 0 , which means there is no 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.
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.
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.
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).
Default global value of timeout
is 0 that means it is infinite.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With