How long will the browser wait after an ajax request? How long will the browser wait after an ajax request? ajax ajax

How long will the browser wait after an ajax request?


If you are using a jQuery $.ajax call you can set the timeout property to control the amount of time before a request returns with a timeout status. The timeout is set in milliseconds, so just set it to a very high value. You can also set it to 0 for "unlimited" but in my opinion you should just set a high value instead.

Note: unlimited is actually the default but most browsers have default timeouts that will be hit.

When an ajax call is returned due to timeout it will return with an error status of "timeout" that you can handle with a separate case if needed.

So if you want to set a timeout of 3 seconds, and handle the timeout here is an example:

$.ajax({    url: "/your_ajax_method/",    type: "GET",    dataType: "json",    timeout: 3000, //Set your timeout value in milliseconds or 0 for unlimited    success: function(response) { alert(response); },    error: function(jqXHR, textStatus, errorThrown) {        if(textStatus==="timeout") {              alert("Call has timed out"); //Handle the timeout        } else {            alert("Another error was returned"); //Handle other error type        }    }});​


Yes and no. Yes the server can do it or be configured to do so, no the browsers (i dont know about version/distributor specifics) may have timeouts enabled.

There are 2 solutions though for achieving/emulating this over HTTP:

  • If this is simple a long running script and you're waiting for results this isnt the way to go, you should instead do as previous poster mentioned and use async processing with server polling for the results, this would be a much more sure fire solution. For example: a thumbnail script from an image processor server side: the user uploads an image, the server immediately returns a 200 and a "Job ID". The client (javascript^^) can then use the JobID to request the job status/result.
  • If your goal is to have something like a realtime connection between browser and server (1 way connection, once the request is made by the browser no further info can be sent without using new requests (ajax^^)), this is called long polling/reverse ajax and can be used for real-time communication over http. There are several techniques using 2 long polled requests in parallel so that once one of them timeout the second one becomes the active and the first one attempts to reconnect.


Can you explain a bit more about what you're trying to achieve - do you have a long running process on a server, do you want to change the settings on just a local machine or are you after a way to manage it for large numbers of users?

How long the browser will wait depends on a number of factors e.g. where the timeout occurs - is it at the TCP level, the server or the local browser?

If you've got a long running process on a server and you want to update a webpage afterwards the typical way to handle it is to run the long process asynchronously and notify the client when it's complete e.g. have an ajax call that polls the server, or use HTTP 1.1 and serve out a notification stream to the client.

In either case it's still possible for the connection to be closed so the client will still need the ability to re-open it.