How long will the browser wait after an ajax request?

How long can the browser wait until an error occurs before the server responds to the request? Can this time be unlimited?

+53
ajax request connection-timeout
Sep 04 2018-11-11T00:
source share
4 answers

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

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

When an ajax call returns due to a timeout, it will return with a timeout error status, which can be handled separately, if necessary.

So, if you want to set the timeout to 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 } } });​ 
+60
04 Oct '11 at 19:57
source share

Yes and no. Yes, the server can do this or is configured for this, browsers (I do not know about the specifications of the version / distributor) can include timeouts.

There are two solutions, though, to achieve / emulate this over HTTP:

  • If this is just a long script and you are waiting for results, this is not the way to go, you should do it like the previous poster and use asynchronous processing with polling the server for results, this will be a much more reliable fire solution. For example: a thumbnail of the script from the server side of the image processor: the user uploads an image that the server simulates 200 and "Job ID". The client (javascript ^^) can then use the JobID to request the status of the job / result.
  • If your goal is to have something similar to a real-time connection between a browser and a server (a one-way connection, once the request is made by the browser, no additional information can be sent without using new requests (ajax ^^) ), this is called long polling / reverse ajax and can be used for real-time communication via http. There are several methods that use two long polls in parallel so that one of them goes into standby mode of the second and the first tries to reconnect.
+13
Oct 03 '11 at 15:44
source share

Can you explain a little more about what you are trying to achieve - do you have a long process on the server, do you want to change the settings only on the local computer, or can you then manage for a large number of users?

How long the browser will wait depends on a number of factors, for example. Where does the timeout take place - is it at the TCP level, server or local browser?

If you have a lengthy process on the server, and you want to refresh a web page, then a typical way of processing it is to asynchronously execute a long process and notify the client when it is completed, for example. have an ajax call that polls the server or uses HTTP 1.1 and serves the client notification stream.

In any case, it is still possible to close the connection so that the client still needs the opportunity to re-open it.

+3
Sep 28 '11 at 1:46 april
source share

I found that in the case of a regular (HTML page) browsers start before the timeout after cca. 30 sec This is important because it is followed by other participants: proxies, routers (do they play this game in a role-playing game? I'm not sure). I use a long delay on the server side for 4 s (if nothing is sent to the client), and my AJAX client performs another HTTP request immediately (I am on the local network, there is no Internet lag). 4 seconds is long enough not to overload the server and network with frequently visited polls, and is short enough for the case when a single poll falls out of a line that the client cannot detect and process.

In addition, there are other problems with comets (lengthy HTTP request): browser restriction on the number of simultaneous HTTP requests, client-side event processing (must be sent immediately to the server), server / network detection and recovery, multi-user processing, etc. d.

+2
04 Oct '11 at 8:16
source share



All Articles