How long will the browser wait after an ajax request?
Asked Answered
D

4

75

How long can the browser wait before an error is shown before server answers for request? Can this time be unlimited?

Dentition answered 4/9, 2011 at 7:2 Comment(5)
Not sure what you are using, but yes you can extend the timeout. To infinite, i don't know. Although i would recommend against doing unlimited time. If you need to, you can always set it to something like 90 seconds or slightly longer. If it takes longer than 30 seconds though, there is probably a better, faster way to do something.Faletti
+1 - I am also curious about this. I suspected that you need to worry about the client timing out on you. You have no control over this. A google search turned up this. It basically says either the server or the browser can time out on you. So I would pick something that the default for all browsers will not exceed. support.microsoft.com/kb/813827Erroneous
When debugging I once went out to lunch after the ajax call and came back to the browser still waiting for the reply. I clicked go on my debugger and the browser picked up the response. I don't think this counts though, because I was debuggingNecromancy
I think he is trying to do reversed ajax...Swen
Possible duplicate "on line chat room based on AJAX": #1192875Hsiuhsu
A
88

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
        }
    }
});​
Apostate answered 4/10, 2011 at 19:57 Comment(1)
Setting timeout value to 0 sets the AJAX timeout to an indefinite period (read unlimited).Franciscka
G
15

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.
Goodall answered 3/10, 2011 at 15:44 Comment(0)
T
3

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.

Triplett answered 28/9, 2011 at 13:46 Comment(0)
A
2

I found, that in case of a normal (HTML page) request, browsers run to timeout after cca. 30 secs. It's important, because other participiants probably follows it: proxies, routers (do routers play in this game? I'm not sure). I am using 4 sec long server-side delay (if there's nothing to send to the client), and my AJAX client performs another HTTP request immediatelly (I am on local network, there's no internet lag). 4 sec is long enough to not to overload the server and network with frequented polls, and is short enough for the case, when somehow one poll falls out of the row which the client can't detect and handle.

Also, there're other issues with comet (long HTTP request): browser's limit on number of simultaneous HTTP request, handling of client-side events (must sent to the server immediatelly), server/network down detection and recovery, multi user handling etc.

Athalee answered 4/10, 2011 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.