Can a number returned by setTimeout() in javascript be negative?

Viewed 1015

Can a number returned by setTimeout() in javaScript be negative?

Currently, I can see that the timeoutId's are 1,2,3,4,5,6.... in Chrome. In Firefox it starts from number 4 and so on.

Is this consistent across browsers and other js engines?

What if the timeoutId number will reach the maximum integer value?

1 Answers

According to Mozilla

The returned timeoutID is a positive integer value

It uses a "pool" of ID's. They may or many not be released back to the pool (it's down to browser implementation - comments suggest they're probably not). To use them all up, you're going to have to do a lot of work and it's almost certainly a programming error.

Note that there is nothing in there to detail how those ID's are generated exactly, so it's not necessarily guaranteed that different browsers will start from the same number. The pool is also shared by setInterval which will effect the ID's.

Related