79

I got a bug report that I can't duplicate, but ajax-call timeout is the current best guess.

So I'm trying to find out the default value for timeout of a jQuery $.ajax() call. Anybody have an idea? Couldn't find it in jQuery documentation.

4

5 Answers 5

53

There doesn't seem to be a standardized default value. I have the feeling the default is 0, and the timeout event left totally dependent on browser and network settings.

For IE, there is a timeout property for XMLHTTPRequests here. It defaults to null, and it says the network stack is likely to be the first to time out (which will not generate an ontimeout event by the way).

Sign up to request clarification or add additional context in comments.

3 Comments

So basically jQuery doesn't use any default value for it. The problem was reported on Safari. I guess I'll just try to force some longish timeout value and hope for the best... Thanks!
@Marcus : did it actually work? I could not find whether timeout always overrides the browser's timeout value (even if your ajax timeout value is bigger than the browser's)
@AdrienBe At the time IIRC I couldn't duplicate the problem from the users bug report, but the timeout issue was the best guess at the moment. So I don't really have an answer.
24

As an aside, when trying to diagnose a similar bug I realised that jquery's ajax error callback returns a status of "timeout" if it failed due to a timeout.

Here's an example:

$.ajax({
    url: "/ajax_json_echo/",
    timeout: 500,
    error: function(jqXHR, textStatus, errorThrown) {
        alert(textStatus); // this will be "timeout"
    }
});

Here it is on jsfiddle.

1 Comment

6

there is no timeout, by default.

2 Comments

Could you share some source supporting your claim.
How can you test the absence of timeout ? It could be really long by default
2

The XMLHttpRequest.timeout property represents a number of milliseconds a request can take before automatically being terminated. The default value is 0, which means there is no timeout. An important note the timeout shouldn't be used for synchronous XMLHttpRequests requests, used in a document environment or it will throw an InvalidAccessError exception. You may not use a timeout for synchronous requests with an owning window.

IE10 and 11 do not support synchronous requests, with support being phased out in other browsers too. This is due to detrimental effects resulting from making them.

More info can be found here.

Comments

-1

In jQuery the default timeout is 0 milliseconds — which actually means “no timeout at all.”
If you don’t set the timeout option yourself (either in the options object you pass to $.ajax() or globally with $.ajaxSetup()), jQuery will leave the request open until:

  1. the server responds, or

  2. the browser/network layer itself decides to abort the underlying HTTP request (each browser has its own longer-running limits).

So, if a caller is seeing a request “time out” while you can’t reproduce it, the culprit is probably either:

  • a custom timeout value set elsewhere in the code (check global $.ajaxSetup calls and per-request settings), or

  • a server/network issue that makes the browser’s own connection timer kick in, not jQuery’s.

To impose an explicit limit, just add (for example) timeout: 15000 for 15 seconds:

$.ajax({   
    url: "/api/data",   
    method: "GET",   
    timeout: 15000,   // 15 seconds   
    success: handleSuccess,   
    error: handleError 
});

2 Comments

That doesn't answer the question as it doesn't state the default value for the timeout.
I modified my answer, please have a look

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.