Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

javascript - What is the maximum delay for setInterval?

I'm having problems on Firefox 15 and Chrome 21 with the following code:

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...