javascriptsetinterval

Why setInterval accepts only 2^31-1 delay values?


According to the specification,

long setInterval(Function handler, optional long timeout, any... arguments);

setInterval() is supposed to accept long timeout delay.

However, on 64bit Linux it behaves like it was a signed 32bit int. I didn't test on other platforms, please try and leave a comment.

The obvious question is - Why?

Can someone explain why do I get instantly output of this:

let maxSigned32 = 2147483647;
let safeInt = maxSigned32 + 1;

console.log(safeInt);
console.log(Number.MAX_SAFE_INTEGER);
console.log(safeInt < Number.MAX_SAFE_INTEGER); // true

let days = Math.round(safeInt / (1000.0 * 60 * 60 * 24));

console.log(safeInt + ' ms is ~' + days + ' days');

setTimeout(() => {
  console.log('I should not see this today')
}, safeInt);

I get this (incorrect?) results both on Chrome 52 and Firefox 48. Interestingly, when I built and tried with latest ChakraCore, it behaves more reasonable.


Solution

  • The long type in Web IDL shouldn't be compared to its C/C++ namesake.

    Its definition can be found here:

    The long type is a signed integer type that has values in the range [−2147483648, 2147483647].