1

Official documentation at jQuery does not mention it.

Possible confusion: I know I can use ajax to gain control over timeout, but my question is different.

Scenario:

I am using post to grab data from a backend which I know will take a long (sometimes very very long) time to load.

Question:

Will my javascript request ever timeout or will it always wait until backend is loaded, even if it takes a few minutes?

Álvaro N. Franz
  • 1,188
  • 3
  • 17
  • 39
  • 1
    If neither the javascript or the server terminates the request it will go on forever within reasonable bounds. Several seconds are completely fine, few minutes are too, hours are most likely not reliable since HTTP is not designed for that. You should find other solutions for that case. – SkryptX Jul 06 '17 at 07:55

3 Answers3

2

HTTP Request timeout is a server side configuration not a client side configuration. Requests submitted via Jquery code is no different. You might want to have a test against the return code from the last request and add exception handling to your code (like resubmit the request)

Always check the response code and a common strategy is to rety. https://www.lifewire.com/g00/troubleshooting-network-error-messages-4102727

Abhijit
  • 1,153
  • 2
  • 16
  • 32
2

The timeout of a request is, by default, controlled by the browser and the receiving server, whichever cancels the request first. I believe most browsers have a 60 second timeout by default. The server can be any arbitrary value.

Will my javascript request ever timeout or will it always wait until backend is loaded, even if it takes a few minutes?

The answer to this is therefore, yes, your request will timeout at an arbitrary point. If you want to control the amount of time you force your users to wait for a request then you can specifically set this time by using the timeout property of the $.ajax call. This overrides any timeout set in the browser or on the server.

15 seconds should be more than enough. If a request is taking longer than that I'd suggest you change the pattern you're using to generate the response.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • 1
    Thank you for your answer. Will accept Ahmad's answer because I think it answers better to my question. I appreciate yours as well ;) – Álvaro N. Franz Jul 06 '17 at 07:57
  • 1
    Not sure what he covered that I didn't, but it's your choice. Hopefully this was of some help – Rory McCrossan Jul 06 '17 at 10:41
  • There have been a few edits to both answers while I was reading them. At the first edits I got the whole view on the case with his answer. I accepted it just so it will be a reference for further users landing on this question. And of course this was of some help. Greatest acceptment would be multi-accept both. Thank you again ;) – Álvaro N. Franz Jul 06 '17 at 10:53
  • No problem - I thought I'd missed something in your question. – Rory McCrossan Jul 06 '17 at 10:53
2

Jquery uses the native XMLHttpRequest module to make requests.

The XMLHttpRequest.timeout property is an unsigned long representing the number of milliseconds a request can take before automatically being terminated. The default value is 0, which means there is no timeout.

Reading the source code of the jquery library, the ajax method does not set a timeout in and way, hence it is save to say that the request does not timeout.

But you can explicitly set a timeout in both jquery and the native module.

this does not mean that your request will not timeout, since the server usually does impose a bail timeout strategy, usually long responses timeout from the server side. you could consider chunking or streaming as a safe and convenient solution.

github jquery ajax source:

https://github.com/jquery/jquery/tree/2d4f53416e5f74fa98e0c1d66b6f3c285a12f0ce/src/ajax

Bamieh
  • 10,358
  • 4
  • 31
  • 52