1

Possible Duplicate:
jquery $.ajax timeout setting

The timeout works, but when the timeout occurs, I want to execute a function. do you have any ideas how to do this?

$.ajax({
    type: "GET", 
    url: "xajax.php", 
    timeout: 100, 
    data: "name=John&location=Boston", 
    success: function(msg) {
        alert( "Data Saved: " + msg );
    }
});
Community
  • 1
  • 1
zapata
  • 61
  • 10

3 Answers3

4

Timeout will be thrown as part of error, you can check if the error reason was timeout and call your function there!

The three arguments for the error handler are xmlhttprequest, textstatus, and message. When a timeout happens, the status arg will be 'timeout'.

$.ajax({
    type: "GET", 
    url: "xajax.php", 
    timeout: 100, 
    data: "name=John&location=Boston", 
    success: function(msg){ alert( "Data Saved: " + msg ); }
    error: function(xmlhttprequest, textstatus, message) {
        if(textstatus==="timeout") {
            alert("Timeout happened"); //run function here!
        } else {
            alert(textstatus);
        }
    }
});​

Possible values for textstatus are (ignoring null) "timeout", "error", "notmodified" and "parsererror".

Related doc is at http://api.jquery.com/jQuery.ajax/

Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
0

Use complete:

A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request ("success", "notmodified", "error", "timeout", "abort", or "parsererror"). As of jQuery 1.5, the complete setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.

or error:

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests. This is an Ajax Event.

Here is an example using complete:

    $.ajax({ 
    type: "GET", 
    url: "xajax.php", 
    timeout: 100, 
    data: "name=John&location=Boston", 
    success: function(msg){ alert( "Data Saved: " + msg ); },
    complete: function(jqXHR, textStatus) { 
        if (textStatus == "timeout") {
           alert('timeout');
        }
    }
    });
amurra
  • 15,221
  • 4
  • 70
  • 87
0

Have a look at the complete function

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

complete(jqXHR, textStatus) A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request ("success", "notmodified", "error", "timeout", "abort", or "parsererror"). As of jQuery 1.5, the complete setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.

$.ajax({ 
   type: "GET", 
   url: "xajax.php", 
   timeout: 100, 
   data: "name=John&location=Boston",
   success: function(msg){ alert( "Data Saved: " + msg ); }, 
   complete: function(jqXHR, textStatus) {
        if (textStatus === "timeout") {
            alert("timeout");
        }
   }
benedict_w
  • 3,543
  • 1
  • 32
  • 49
  • i did try like a kid :) but it doesnt work!... timeout: function(){alert("timeout!!!");}..........i try error: function(x,t,m){alert(t);}...it doesnt works. because it is not JSON dataType! – zapata Oct 22 '12 at 11:09