window.setInterval 是否有最大延迟限制
Is there a maximum delay limit for window.setInterval
今天 window.setInterval 我遇到了一个有趣的问题。当使用足够长的延迟(在本例中为 30 天的毫秒数)时,它每秒执行一次,而不是每 30 天执行一次。在最新的 Chrome 和 Firefox 中测试。
window.setInterval(function() {
document.getElementById("first").innerHTML = new Date().toString();
}, 5000);
window.setInterval(function() {
document.getElementById("second").innerHTML = new Date().toString();
}, 2592000000);
我找不到任何关于 setInterval 延迟最大值的权威文档,MDN documentation 也没有提到任何内容。其他在线资源建议延迟应该能够容纳任何带符号的 32 位整数。
window.setInterval中的延迟参数是否有最大值,是多少?
根据 setTimeout
documentation on the public wiki MDN 确实有一个最大值,虽然它似乎没有 "official" - 限制是一个带符号的 32 位整数。
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.
2592000000
的值确实大于2147483647
从而导致溢出。
今天 window.setInterval 我遇到了一个有趣的问题。当使用足够长的延迟(在本例中为 30 天的毫秒数)时,它每秒执行一次,而不是每 30 天执行一次。在最新的 Chrome 和 Firefox 中测试。
window.setInterval(function() {
document.getElementById("first").innerHTML = new Date().toString();
}, 5000);
window.setInterval(function() {
document.getElementById("second").innerHTML = new Date().toString();
}, 2592000000);
我找不到任何关于 setInterval 延迟最大值的权威文档,MDN documentation 也没有提到任何内容。其他在线资源建议延迟应该能够容纳任何带符号的 32 位整数。
window.setInterval中的延迟参数是否有最大值,是多少?
根据 setTimeout
documentation on the public wiki MDN 确实有一个最大值,虽然它似乎没有 "official" - 限制是一个带符号的 32 位整数。
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.
2592000000
的值确实大于2147483647
从而导致溢出。