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
1 Answer
Reset to default 10According 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
版权声明:本文标题:javascript - Is there a maximum delay limit for window.setInterval - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741905527a2404118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论