What is minimum millisecond value of setTimeout?
I would like to put
var minValue = 0;
if ( typeof callback == 'function' ) {
setTimeout( callback, minValue );
}
this code when I implement callback function with JavaScript.
But I've found that modern browsers and some old browsers
have different minimum timeout value.
I know that Zero cannot be minimum value.
What would be minimum value of setTimeout for
modern browsers and some old browsers for compatibility issues?
Solution 1:
I think that 10 will be the most reliable minimum in all browser, since I've seen a lot of codes using it.
However, 4ms is the minimum for HTML5
In fact, 4ms is specified by the HTML5 spec and is consistent across browsers released in 2010 and onward. Prior to (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2) , the minimum timeout value for nested timeouts was 10 ms.
Solution 2:
The minimum is 4ms (as of HTML5) in modern browser, prior to that, it was 10ms. Note that these times are never 100% accurate.
Solution 3:
setTimeout
is most probably calling the sleep
or Sleep
system call.
The actual mechanics, including the minimum amount of milliseconds, of setTimeout
are proprietary and/or system-dependent, since they are not in the official ECMA specs. It depends on your Javascript run-time, as well as the system you are running it on. Given, your Javascript run-time does not add a whole lot of overhead, the minimum amount of milliseconds is determined by the timeslice resolution of your operating system and hardware. The smallest "sleepable" amount of time is usually the time it takes for the process to be allocated another timeslice by your system's scheduling algorithm.
On Windows (post XP) for example, the documentation for the sleep system call reveals:
A value of zero causes the thread to relinquish the remainder of its time slice to any other thread that is ready to run. If there are no other threads ready to run, the function returns immediately, and the thread continues execution.
That means, under some extremely rare conditions, where there is no other process currently waiting on the hardware thread that your Javascript run-time process is running on, it might continue immediately after the caller finished executing, depending on how your Javascript run-time is implemented. You will probably not observe such condition very often though :)