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
.
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
.
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
.
The highest possible interval, 2**31-1ms
is a little shy of 25 days, so more than enough for anything reasonable.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…