Nilesh Pethani
Nilesh Pethani

Reputation: 822

Why ajax call's timeout not working properly

In my ASPX page I call a javascript function which contains an Ajax call with 5 seconds timeout.

I first try to call this function with internet connection off and timeout works properly. But when I call this function for the second time, Ajax call directly calls success function, without waiting for timeout and subsequent error function.

Here is the the ajax call

$.ajax({
     type: "POST",
     url: pageUrl + "/SubmitAnsGetNewQue",
     data: jsonParamsForSbmt,
     timeout: 5000,
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     async: true,
     success: SetNextQueSet,
     error: OnErrorCall
});

Upvotes: 1

Views: 6214

Answers (1)

gdoron
gdoron

Reputation: 150243

timeout isn't what you think in this context, it's the amount of time in miliseconds that jQuery will wait for the ajax request to return.

If it took more than 5 seconds to the server to reply, the callbacks won't fire.

timeout

Set a timeout (in milliseconds) for the request. This will override any global timeout set with $.ajaxSetup(). The timeout period starts at the point the $.ajax call is made; if several other requests are in progress and the browser has no connections available, it is possible for a request to time out before it can be sent. In jQuery 1.4.x and below, the XMLHttpRequest object will be in an invalid state if the request times out; accessing any object members may throw an exception. In Firefox 3.0+ only, script and JSONP requests cannot be cancelled by a timeout; the script will run even if it arrives after the timeout period.

docs

Upvotes: 3

Related Questions