Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can setTimeout be too long?

Tags:

javascript

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?

like image 826
flyingarmadillo Avatar asked Sep 10 '12 12:09

flyingarmadillo


People also ask

Is there a limit for setTimeout?

Maximum delay value Browsers including Internet Explorer, Chrome, Safari, and Firefox store the delay as a 32-bit signed integer internally. This causes an integer overflow when using delays larger than 2,147,483,647 ms (about 24.8 days), resulting in the timeout being executed immediately.

Does setTimeout cause performance issues?

No significant effect at all, setTimeout runs in an event loop, it doesn't block or harm execution.

How long is setTimeout?

I just ran into a problem with setTimeout and setInterval . Turns out the delay (in milliseconds) for these functions is a 32 bit signed quantity, which limits it to 231-1 ms (2147483647 ms) or 24.855 days.

What happens if setTimeout is not cleared?

In other words, it's a no-op; nothing happens, and no error will be thrown. Save this answer. Show activity on this post. You don't actually need to use clearTimeout , you only use it if you wish to cancel the timeout you already set before it happens.


2 Answers

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!
like image 156
sajawikio Avatar answered Sep 30 '22 02:09

sajawikio


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');
}
like image 40
Curtis Avatar answered Sep 30 '22 02:09

Curtis