What is default timeout in setTimeout() method of JavaScript Window object?

The HTML5 Timer spec says:

5) Let timeout be the second method argument, or zero if the argument was omitted.


If you don't want to set a timeout then just pass 0

setTimeout(function() {
    ...
}, 0);

Alternatively you can just omit the delay parameter and 0 is assumed, however, for robustness (and general readability) it's best to pass something in there.

Note - this does not guarantee that your code will run immediately. It queues your task to run whenever the UI thread is available to process it.


Why don't you check it out for yourself? Documentation is always welcome, however you can't beat real-world examples:

var startTime = Date.now();
setTimeout(function () {
    var endTime = Date.now();
    alert("timeOut was: " + (endTime - startTime) + "ms");
});

JSFiddle: http://jsfiddle.net/losnir/PbJBA/
Have a go by playing with different timeout values.

Not passing an a timeout arguments is practically the same as passing a 0. However, as you can see, the timeout is never 0, that is because your callback function will be placed in a queue, and executed as soon as possible (given your requested timeout value).

It goes to about 5ms on average, and if your UI thread is doing some intensive tasks, or your CPU is busy - it can go as high as a couple hundreds of milliseconds, atrocious!

Tags:

Javascript