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 } } });
Tj Kellie 04 Oct '11 at 19:57 2011-10-04 19:57
source share