Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax timeout callback function

Tags:

Is there a way to run a function if jQuery's $.ajax function hits it's timeout?

i.e.

$.ajax({ ... ... ,timeout:1000(){do something if timeout) ...  }); 
like image 986
d-_-b Avatar asked May 14 '13 00:05

d-_-b


People also ask

What is the default timeout for AJAX call?

The default value is 0 , which means there is no timeout.

Is AJAX callback function?

In Ajax functions many callback functions exist. For example, when the request is raised, one function will raise one event when the request is successfully finished, again another function will be fired. This is the most important and heavily used functions of jQuery Ajax functions.

Which of the following adds a timeout to an AJAX request using jQuery?

This can be achieved by using jQuery setTimeout() function. This function executes the given Ajax code after some amount of given time.


1 Answers

$.ajax({     ...     timeout: 1000,     error: function(jqXHR, textStatus, errorThrown) {         if(textStatus==="timeout") {            //do something on timeout         }      } });​ 

For more information check out the jQuery documentation:

http://api.jquery.com/jQuery.ajax/


Edited

It's been over a year since I initially answered this and the textStatus possible values have changed to "success", "notmodified", "error", "timeout", "abort",or"parsererror". For error callbacks, only the last four statuses are possible.

Also you can now wire your error handlers through the returned JQuery deferred promise object's .fail method:

var promise = $.ajax({ timeout: 1000 });  promise.fail(function(jqXHR, textStatus) {     if(textStatus==="timeout") {         // handle timeout       } }); 
like image 175
Dan-Nolan Avatar answered Sep 28 '22 23:09

Dan-Nolan