2

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.

Jed Fox
  • 2,979
  • 5
  • 28
  • 38
Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85
  • 2
    In fact, according to specification of Number: "The maximum safe integer in JavaScript (2^53 - 1)." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number, – Andrew Paramoshkin Aug 26 '16 at 20:51
  • @AndrewParamoshkin correct, it's in the snippet BTW – Michał Šrajer Aug 26 '16 at 20:56
  • 2
    *"Note that the bitwise operators and shift operators operate on 32-bit ints, so in that case, the max safe integer is 2^31-1, or 2147483647."* from [here](http://stackoverflow.com/questions/307179/what-is-javascripts-highest-integer-value-that-a-number-can-go-to-without-losin) – Andrew Li Aug 26 '16 at 20:59
  • @AndrewL. I know that, but there is non of such operator used that would call EcmaScript'a defined ToInt32() conversion function. – Michał Šrajer Aug 26 '16 at 21:19

1 Answers1

5

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].

robertklep
  • 198,204
  • 35
  • 394
  • 381