16

I am creating an app that polls the server for specific changes. I use a self calling function using setTimeout. Something like this basically:

<script type="text/javascript">
someFunction();

function someFunction() {
  $.getScript('/some_script');
  setTimeout(someFunction, 100000);
}
</script>

In order to make this polling less intensive on the server, I want to have a longer timeout interval; Maybe somewhere within the 1min to 2min range. Is there a point at which the timeout for setTimeout becomes too long and no longer works properly?

flyingarmadillo
  • 2,131
  • 5
  • 21
  • 37
  • I don't think it'd be anywhere near the number you'd be putting in it. – Jeremy Rodi Sep 10 '12 at 12:18
  • 1
    Possible duplicate: http://stackoverflow.com/questions/3468607/why-does-settimeout-break-for-large-millisecond-delay-values – Richard Sep 10 '12 at 12:20
  • As Richard pointed out, there is a perfect answer to your question, in case you had really large timeouts (larger than ~24 days): http://stackoverflow.com/questions/3468607/why-does-settimeout-break-for-large-millisecond-delay-values#18182660 – Alexis Wilke Jun 29 '16 at 04:04

2 Answers2

16

You are technically OK. You can have a timeout of up to 24.8611 days!!! if you really want to. setTimeout can be up to 2147483647 milliseconds (the max for 32 bit integer, and that's about 24 days) but if it is higher than that you will see unexpected behavior. See Why does setTimeout() "break" for large millisecond delay values?

For intervals, like polling, I recommend using setInterval instead of a recursive setTimeout. setInterval does exactly what you want for polling, and you have more control too. Example: To stop the interval at any time, make sure you stored the return value of setInterval, like this:

var guid = setInterval(function(){console.log("running");},1000) ;
//Your console will output "running" every second after above command!

clearInterval(guid) 
//calling the above will stop the interval; no more console.logs!
Community
  • 1
  • 1
sajawikio
  • 1,516
  • 7
  • 12
  • 1
    I disagree about setInterval over recursive setTimeout. The setTimeout should be in an if though, to stop it from being called again if/when it needs to stop. https://zetafleet.com/blog/2010/04/why-i-consider-setinterval-to-be-harmful.html – LocalPCGuy Apr 19 '17 at 13:52
4

setTimeout() uses a 32bit integer for its delay parameter. Therefore the maximum is:

2147483647

Rather than using a recursive setTimeout() I recommend using setInterval():

setInterval(someFunction, 100000);

function someFunction() {
   $.getScript('/some_script');
}
Curtis
  • 101,612
  • 66
  • 270
  • 352
  • 4
    I disagree about setInterval over recursive setTimeout. The setTimeout should be in an if though, to stop it from being called again if/when it needs to stop. https://zetafleet.com/blog/2010/04/why-i-consider-setinterval-to-be-harmful.html – LocalPCGuy Apr 19 '17 at 13:52
  • 1
    With recursive setTimeout you make sure if you put it at the end of your function to call it only if the rest of the function was already done. setInterval will call every interval without possibility for you to make sure last call was finished. In some case it's important, in others we don't care but I think it's good to be aware of this. – babar78 Aug 23 '18 at 16:16