What is the minimum millisecond of setTimeout value?

I would like to put

var minValue = 0; if ( typeof callback == 'function' ) { setTimeout( callback, minValue ); } 

this code when i implement the callback function using javascript.

But I found that modern browsers and some older browsers

have a different minimum timeout value.

I know that Zero cannot be the minimum value.

What would be the minimum setTimeout value for

modern browsers and some old browsers for compatibility issues?

+41
javascript cross-browser timer settimeout
Mar 10 2018-12-12T00:
source share
4 answers

I think that 10 will be the most reliable minimum in the whole browser, since I have seen many codes using it.

However 4ms is minimal for HTML5

In fact, 4ms is indicated by the HTML5 specification and is consistent between browsers released in 2010 and beyond. Prior to (Firefox 5.0 / Thunderbird 5.0 / SeaMonkey 2.2), the minimum timeout value for nested timeouts was 10 ms.

+40
Mar 10 2018-12-12T00:
source share

In a modern browser, at least 4 ms (with HTML5), before that - 10 ms. Please note that these times are never 100% accurate.

+9
Mar 10 2018-12-12T00:
source share

This article analyzes the performance graphs of Firefox, Safari, and Opera, and the graphs:

http://ejohn.org/blog/analyzing-timer-performance/

Firefox 2, Opera and Safari have a 10 ms bottom window for delays

For older browsers, you can run a test similar to the one in this article. I just checked the test I had with setInterval at setInterval intervals in IE6 and I got an average of 55ms. setTimeout seems 35 ms lower.

I ran a test in Chromium and got ~ 11 ms on average in 10 ms timeout. I tried it with an interval of 4 ms and 1 ms and got ~ 4.5 ms for both. Also, keep in mind that numbers may vary across operating systems.

If you're interested, here is the test code:

 <script> // number of times to call setTimeout before calculating average var ITERATIONS = 200; window.onload = function() { testTimeout(10, +new Date, 0, 0); } // calls setTimeout repeatedly at a specified interval, tracking the amount // of time that passes between successive calls function testTimeout(interval, last, sum, ii) { var time = +new Date; var difference = time - last; sum += difference; if (ii % ITERATIONS == 1) { document.body.innerHTML = sum / ITERATIONS; sum = 0; } window.setTimeout( function() { testTimeout(interval, time, sum, ii + 1) }, interval); } </script> 
+3
Mar 10 2018-12-12T00:
source share

setTimeout most likely calls a sleep or Sleep call.

Actual mechanics, including the minimum number of milliseconds, setTimeout is patented and / or system dependent as they are not part of the official ECMA specifications. It depends on the execution time of Javascript, as well as the system on which you run it. Given that a lot of overhead is not added during Javascript execution, the minimum number of milliseconds is determined by the time resolution of your operating system and hardware. The smallest dormant amount of time is usually the time it takes for a process to be assigned a different time layer using your system planning algorithm .

For example, on Windows (post XP), the documentation for a system sleep call shows:

A value of zero causes the thread to discard the remainder of its temporary fragment to any other thread that is ready to start. If there are no other threads ready to start, the function returns immediately and the thread continues execution.

This means that in some extremely rare conditions, when there is no other process in the hardware thread that runs your Javascript process that expects the current Javascript execution process , it can continue immediately after the caller completes, depending on how running javascript runtime. You probably won't observe this condition very often, though :)

+2
Nov 25 '14 at 12:29
source share



All Articles