By InspiredJW


2012-03-10 14:29:54 8 Comments

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?

4 comments

@Domi 2014-11-25 12:29:23

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 :)

@Pinal 2015-05-14 11:39:48

real true answer

@Davide 2015-12-11 10:35:04

I must admit this answer does not convince me, as it doesn't match with what I think I know about javascirpt (mostly-)single-threaded event-based execution. Could you point to any reference saying that any js implementation calls sleep?

@nnnnnn 2016-07-20 14:17:05

"it might continue immediately, depending on how your Javascript run-time is implemented" - If the runtime is implemented correctly the timeout won't run until after the current code finishes executing - even if you specify a time of 0 it will still queue.

@Iharob Al Asimi 2018-09-30 20:12:51

I highly doubt it. Specially sleep|Sleep system calls being involved with this. I suppose javascript has it's own scheduling mechanism and the granularity will ultimately depend on the particular browser implementation of the language. But javascript is event based and I think it would be checking for events in a loop and if there are any scheduled and "expired" execute them. That's why it's not accurate (milliseconds are large in this context, so accuracy should be expected) and also why there should be a limit for setTimeout()'s time parameter.

@Iharob Al Asimi 2018-09-30 20:18:46

@Devourant 2012-03-10 15:01:54

This article tests Firefox, Safari, and Opera and plots performance graphs:

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

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

For older browsers, you can do a test like the one in that article. I just ran a test that I had from a while ago of setInterval using a 10ms interval in IE6, and I got an average of 55ms. setTimeout seems to be lower at 35ms.

I ran the test in Chromium and got ~11ms average for a 10ms timeout. I tried it with 4ms and 1ms intervals and got ~4.5ms for both. Also, keep in mind that the numbers could vary among operating systems.

If you're interested, here's 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>

@Madara Uchiha 2012-03-10 14:32:14

The minimum is 4ms (as of HTML5) in modern browser, prior to that, it was 10ms. Note that these times are never 100% accurate.

@Martin. 2012-03-10 14:31:10

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.

@Tobbe Brolin 2013-12-10 21:22:08

I´m sorry, this is not (no longer?) true. I just cannot find a statement about a 4ms delay in the standard, except for a note: "Note: Timers can be nested; after five such nested timers, however, the interval is forced to be at least four milliseconds." Further curiosity led me to this great post on google groups about how they do it in Chrome.

@apsillers 2014-01-15 02:29:49

@TobbeBrolin Whether it's true in implementation, it is still in the spec: "If the currently running task is a task that was created by the setTimeout() method, and timeout is less than 4, then increase timeout to 4."

@apsillers 2014-01-15 12:59:14

@TobbeBrolin However, I don't see that langauge reflected in WHATWG's spec. Looks like the W3C and WHATWG disagree on this.

Related Questions

Sponsored Content

73 Answered Questions

[SOLVED] How can I get query string values in JavaScript?

64 Answered Questions

[SOLVED] What is the most efficient way to deep clone an object in JavaScript?

25 Answered Questions

17 Answered Questions

[SOLVED] Why is setTimeout(fn, 0) sometimes useful?

33 Answered Questions

[SOLVED] What is the !! (not not) operator in JavaScript?

  • 2009-04-24 08:13:58
  • Hexagon Theory
  • 410244 View
  • 2476 Score
  • 33 Answer
  • Tags:   javascript operators

20 Answered Questions

[SOLVED] What is the difference between call and apply?

20 Answered Questions

[SOLVED] How can I pass a parameter to a setTimeout() callback?

7 Answered Questions

[SOLVED] What is JSONP all about?

50 Answered Questions

12 Answered Questions

Sponsored Content