Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a maximum delay limit for window.setInterval

Today I encountered an interesting problem with window.setInterval. When used with a sufficiently large delay (in this case the number of milliseconds in 30 days) it executes every second instead of every 30 days. Tested in latest Chrome and Firefox.

jsFiddle link

window.setInterval(function() {
    document.getElementById("first").innerHTML = new Date().toString();
}, 5000);
window.setInterval(function() {
    document.getElementById("second").innerHTML = new Date().toString();
}, 2592000000);

I couldn't find any authoritative documentation on the max value of a delay in setInterval, and the MDN documentation doesn't mention anything. Other sources online suggest that delay should be able to accommodate any signed 32-bit integer.

Does the delay parameter in window.setInterval have a maximum value and what is it?

like image 597
tomfumb Avatar asked Aug 17 '16 22:08

tomfumb


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.

Is setInterval better than setTimeout?

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.

How many times does setInterval run?

When your code calls the function repeatEverySecond it will run setInterval . setInterval will run the function sendMessage every second (1000 ms).

Why is setInterval not accurate?

Why are setTimeout and setInterval not accurate? To answer this question, you need to understand that there is a mechanism called event loop in the JavaScript host environment (browser or Node. js). It is necessary for front-end developers to understand this mechanism.


1 Answers

According to the setTimeout documentation on the public wiki MDN there is indeed a maximum, though it doesn't seem "official" - the limitation is a signed 32 bit integer.

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 2147483647, resulting in the timeout being executed immediately.

The value of 2592000000 is indeed larger than 2147483647 thus causing the overflow.

like image 80
VLAZ Avatar answered Oct 14 '22 00:10

VLAZ