javascript setInterval 的最大延迟是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12633405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the maximum delay for setInterval?
提问by Nogwater
I'm having problems on Firefox 15 and Chrome 21 with the following code:
我在 Firefox 15 和 Chrome 21 上遇到问题,代码如下:
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?
在这两种浏览器上,立即运行的功能会非常快速地重复。当然,这是一个很大的数字(代表从现在开始大约 10 年),但我不希望它被视为一个很小的或负数。我在任何文档中都没有看到最大允许延迟。有谁知道是否有标准最大值,或者这只是浏览器很有趣吗?
采纳答案by Pointy
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.
我目前找不到任何文档,但如果计时器值必须适合 32 位有符号整数,我不会感到惊讶。
回答by Aaron Dufour
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
.
间隔存储在一个有符号的 32 位整数中(在测试的实现中:谷歌浏览器中的 V8),所以你看到的行为是间隔溢出到负数的结果(在这种情况下,它的行为就像间隔是0
)。因此,您可以使用的最大间隔是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
.
表现得像间隔是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
.
表现得像间隔是1000
(一秒)。在这里,2**33
不影响前 32 位,所以我们只得到1000
.
The highest possible interval, 2**31-1ms
is a little shy of 25 days, so more than enough for anything reasonable.
可能的最高间隔时间2**31-1ms
略短于 25 天,因此对于任何合理的事情来说都绰绰有余。
回答by rekire
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.
我认为最大延迟是 2 31-1,即 2,147,483,647ms。以毫秒为单位的有符号 32 位整数的最大值。如果它是无符号的,它将是 2 32-1 = 4,294,967,295。
回答by Xin
Max is 2,147,483,647
(231-1)
最大值是2,147,483,647
(2 31-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