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?

Nogwater
  • 2,777
  • 3
  • 28
  • 28

6 Answers6

30

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.

Aaron Dufour
  • 17,288
  • 1
  • 47
  • 69
  • The interval *in that particular implementation* is stored in .. but +1 for showing the methodology used. –  Sep 28 '12 at 04:32
  • Nice example tests. I basically did the same thin in my comment on the question itself, but +1 for showing your work. :) – Nogwater Sep 28 '12 at 04:37
12

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
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.

rekire
  • 47,260
  • 30
  • 167
  • 264
2

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
Xin
  • 33,823
  • 14
  • 84
  • 85
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;
};
  • 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. – Andrew Nov 10 '21 at 05:53
1

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

sudazzle
  • 527
  • 4
  • 8