DEV Community

Discussion on: What is the oddest JavaScript behavior?

Collapse
 
alexanderholman profile image
Alexander Holman • Edited

A colleague the other day noticed that the setTimeout/setInterval takes a 32 bit signed int as an argument, which is fine except that (as far as I know) JS doesn't natively support integers... but but instead Numbers are specifically 8 byte signed doubles. If you happen to have a interval or timeout every 2^31 miliseconds (ish) or more, then the value is interpreted as a negative number and therefore executes instantly and in terms of intervals repeatedly. So timeouts and intervals are limited to just under 25 days.

Collapse
 
shayd16 profile image
Shayne Darren

I'm guessing a web app won't need a 25-day timeout or interval.

I tried a very basic test on node and it seems to be waiting on it to execute... any idea why?

Collapse
 
alexanderholman profile image
Alexander Holman • Edited

not sure... i just ran it in node and experienced the same issue as the browser. The following line should make node print without pausing:

setInterval(() => {console.log('log')}, 2**31)

or

setInterval(() => {console.log('log')}, -1)

where the following will take 596.5 hours (roughly):

setInterval(() => {console.log('log')}, 2**31-1)

I am currently on a Windows machine running v8.9.1, perhaps its solved in later versions?