admin管理员组

文章数量:1312922

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 acmodate any signed 32-bit integer.

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

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 acmodate any signed 32-bit integer.

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

Share Improve this question asked Aug 17, 2016 at 22:12 tomfumbtomfumb 3,7593 gold badges38 silver badges50 bronze badges 4
  • A note: the standard does not restrict on type/ranges for the timeout argument w3/TR/2011/WD-html5-20110525/timers.html#timers (correct me if I'm wrong though) – zerkms Commented Aug 17, 2016 at 22:20
  • It does indeed seem to be at 2147483648 (the smallest positive integer that isn't a signed 32-bit integer) where it first occurs. – Paul Commented Aug 17, 2016 at 22:22
  • 1 Explained in Aaron Dufour's answer here: stackoverflow./questions/12633405/… – Wake Commented Aug 17, 2016 at 22:25
  • Does not happen in Opera 12. – Bergi Commented Aug 18, 2016 at 20:11
Add a ment  | 

1 Answer 1

Reset to default 10

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.

本文标签: javascriptIs there a maximum delay limit for windowsetIntervalStack Overflow