Determine if $.ajax error is a timeout Determine if $.ajax error is a timeout ajax ajax

Determine if $.ajax error is a timeout


If your error event handler takes the three arguments (xmlhttprequest, textstatus, and message) when a timeout happens, the status arg will be 'timeout'.

Per the jQuery documentation:

Possible values for the second argument (besides null) are "timeout", "error", "notmodified" and "parsererror".

You can handle your error accordingly then.

I created this fiddle that demonstrates this.

$.ajax({    url: "/ajax_json_echo/",    type: "GET",    dataType: "json",    timeout: 1000,    success: function(response) { alert(response); },    error: function(xmlhttprequest, textstatus, message) {        if(textstatus==="timeout") {            alert("got timeout");        } else {            alert(textstatus);        }    }});​

With jsFiddle, you can test ajax calls -- it will wait 2 seconds before responding. I put the timeout setting at 1 second, so it should error out and pass back a textstatus of 'timeout' to the error handler.

Hope this helps!