Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setInterval with Infinity

Tags:

javascript

So If i am doing:

setInterval(function(){

   console.log("1");

},Infinity);

It keeps on logging 1 as if it is a for loop. Why is that behaviour?

like image 999
void Avatar asked Dec 18 '15 08:12

void


1 Answers

When the float/number Infinity needs to be converted to a 32-bit integer value in JavaScript, as it does for setTimeout, it's converted to zero:

console.log(typeof Infinity); // number
console.log(Infinity | 0);    // 0

ECMA-262 6e Section 7.1.5 ToInt32 ( argument )

The abstract operation ToInt32 converts argument to one of 232 integer values in the range −231 through 231−1, inclusive. This abstract operation functions as follows:

  1. Let number be ToNumber(argument).
  2. ReturnIfAbrupt(number).
  3. If number is NaN, +0, −0, +∞, or −∞, return +0.
  4. [...]
like image 125
Jeremy Avatar answered Nov 04 '22 11:11

Jeremy