Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How long will the browser wait after an ajax request?

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

like image 843
Taras Lukavyi Avatar asked Sep 04 '11 07:09

Taras Lukavyi


People also ask

How wait until AJAX is done?

As other answers mentioned you can use ajaxStop() to wait until all ajax request are completed. If you want do it for an specific ajax() request the best you can do is use complete() method inside the certain ajax request: $.

How do I know if AJAX request is successful?

$. ajax({ url: "page. php", data: stuff, success: function(response){ console. log("success"); } });

Why does AJAX take so much time?

If this is a busy server or overloaded server, then it could be that your app is swapped out of memory or the cache is empty and it takes a longer time to get your app up and running and ready to start processing the response.

Does AJAX call 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.


2 Answers

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         }     } });​ 
like image 108
Tj Kellie Avatar answered Sep 28 '22 14:09

Tj Kellie


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.
like image 42
smassey Avatar answered Sep 28 '22 15:09

smassey