Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum delay for setInterval?

I'm having problems on Firefox 15 and Chrome 21 with the following code:

setInterval(function () { console.log('test') }, 300000000000)

On both browsers, the function is run right away repeats very quickly. Sure, that's a big number (representing about 10 years from now), but I wouldn't expect it to be treated as a tiny or negative number. I haven't seen a maximum allowed delay in any documentation. Does anyone know if there's a standard max, or if this is just the browsers being funny?

like image 580
Nogwater Avatar asked Sep 28 '12 04:09

Nogwater


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.

How do you delay setInterval?

The setInterval method is used to schedule a function to be executed repeatedly after a period of time. The syntax for this method is this: const timerID = setInterval(function, delay, arg1, arg2, ...) In this case, the delay is the time in milliseconds that the timer should delay successive executions of the function.

Which is better setTimeout or setInterval?

setTimeout allows us to run a function once after the interval of time. setInterval allows us to run a function repeatedly, starting after the interval of time, then repeating continuously at that interval.

Why is setInterval not accurate?

The real-time interval can only be greater than or equal to the value we passed. From the above code, we can see that setInterval is always inaccurate. If time-consuming tasks are added to the code, the difference will become larger and larger ( setTimeout is the same).


4 Answers

The interval is stored in a signed 32-bit int (in the tested implementation: V8 in Google Chrome), so the behavior you're seeing is the result of the interval overflowing to a negative number (in which case it behaves as if the interval was 0). Thus, the maximum interval that you can use is 2**31 - 1.

Here's how I determined that this was the case:

setInterval(function(){console.log("hi");}, Math.pow(2,31)); 

Behaves like the interval is 0.

setInterval(function(){console.log("hi");}, Math.pow(2,31) - 1); 

Doesn't fire in the time I was willing to wait.

setInterval(function(){console.log("hi");}, Math.pow(2,33) + 1000); 

Behaves like the interval is 1000 (one second). Here, the 2**33 doesn't affect the first 32 bits, so we get just 1000.

The highest possible interval, 2**31-1ms is a little shy of 25 days, so more than enough for anything reasonable.

like image 164
Aaron Dufour Avatar answered Sep 21 '22 13:09

Aaron Dufour


I can't find any documentation at the moment, but I wouldn't be surprised if the timer value had to fit in a 32-bit signed integer.

like image 40
Pointy Avatar answered Sep 18 '22 13:09

Pointy


I think that the maximum delay is 231-1 which is 2,147,483,647ms. The maximum value of a signed 32 bit integer in ms. If it would be unsigned it would be 232-1 = 4,294,967,295.

like image 39
rekire Avatar answered Sep 20 '22 13:09

rekire


Max is 2,147,483,647 (231-1)

Be careful that if you make the number bigger than that, it will run immediately (Imaging that you put a negative value, so the browser will run infinitely loop)

setInterval(()=>console.log('n'),2147483647)
31
setInterval(()=>console.log('y'),2147483648)
38
(1588) y
like image 21
Xin Avatar answered Sep 20 '22 13:09

Xin