What is the maximum delay for setInterval?
Asked Answered
T

6

25

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?

Theotokos answered 28/9, 2012 at 4:3 Comment(7)
I suspect that the number may be coerced to a 32-bit integer somewhere along the way ...Sendoff
@Pointy, that's probably a good guess. Hmm... :-/Theotokos
@Pointy. It looks like 2^31-1 is safe, but 2^31 breaks, so you're almost definitely right. If you post this as an answer, I'll give you the karma.Theotokos
Is that the case regardless of which machine the code is being run on?Thayer
@Gareth. Good question. I've only tested on a couple computers.Theotokos
"What's the maximum delay for setInterval?", 2**32ms ~ 50 days.Aubert
It's uses a signed integer, so the maximum delay is half of that, 24.855 days. When delay is larger than 2147483647 or less than 1, the delay will be set to 1.Liveried
S
13

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.

Sendoff answered 28/9, 2012 at 4:14 Comment(4)
This is correct. setInterval() fails with values larger than 32-bit signed int.Blinker
2147483647 ms developer.mozilla.org/en-US/docs/Web/API/…Apthorp
@Apthorp which is the largest positive value you can fit in a 32-bit signed integer.Sendoff
@Pointy. A 32-bit signed integer value range [-2147483648 to 2147483647]Apthorp
L
34

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.

Lundy answered 28/9, 2012 at 4:25 Comment(2)
The interval in that particular implementation is stored in .. but +1 for showing the methodology used.Brodeur
Nice example tests. I basically did the same thin in my comment on the question itself, but +1 for showing your work. :)Theotokos
S
13

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.

Sendoff answered 28/9, 2012 at 4:14 Comment(4)
This is correct. setInterval() fails with values larger than 32-bit signed int.Blinker
2147483647 ms developer.mozilla.org/en-US/docs/Web/API/…Apthorp
@Apthorp which is the largest positive value you can fit in a 32-bit signed integer.Sendoff
@Pointy. A 32-bit signed integer value range [-2147483648 to 2147483647]Apthorp
C
6

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.

Cephalochordate answered 28/9, 2012 at 4:14 Comment(1)
2 Billions 147 Millions.Scend
D
3

Max is 2,147,483,647 (231-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
Drynurse answered 29/8, 2017 at 1:4 Comment(0)
M
2

If you need an interval larger than 2,147,483,647 here's an example in TypeScript that will allow you to set an interval for a max of 458,496,310,632,933,156,516.92 days:

Obviously, I have not tested that this works for that long :D.

export const setLongInterval = (callback: any, timeout: number, ...args: any): Timeout => {
    let count = 0;
    const MAX_32_BIT_SIGNED = 2147483647;
    const maxIterations = timeout / MAX_32_BIT_SIGNED;

    const onInterval = () => {
        ++count;
        if (count > maxIterations) {
            count = 0;
            callback(args);
        }
    };

    return setInterval(onInterval, Math.min(timeout, MAX_32_BIT_SIGNED));
};

export const setLongTimeout = (callback: any, timeout: number, ...args: any): Timeout => {
    let count = 0;
    let handle: Timeout;
    const MAX_32_BIT_SIGNED = 2147483647;
    const maxIterations = timeout / MAX_32_BIT_SIGNED;

    const onInterval = () => {
        ++count;
        if (count > maxIterations) {
            count = 0;
            clearInterval(handle);
            callback(args);
        }
    };

    handle = setInterval(onInterval, Math.min(timeout, MAX_32_BIT_SIGNED));
    return handle;
};
Minneapolis answered 1/5, 2021 at 22:34 Comment(1)
I have been running a test program since you posted your answer, and the interval has finally just fired off. I can confirm that it works. Kidding.Friedrich
A
2

The delay argument is converted to a signed 32-bit integer. This effectively limits delay to 2147483647 ms, since it's specified as a signed integer in the IDL.

It is mentioned in the documentation

Apthorp answered 25/1, 2022 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.