Amine Hajyoussef
Amine Hajyoussef

Reputation: 4430

is there a difference between setTimeout(fun) and setTimeout(fun,4)

the behavior of setTimeout is a bit confusing, all resources I've read indicate that when no delay is specified, the task is appended to the end of queue and executed when the interpreter has nothing left to do (empty queue?). however, take the following example:

setTimeout(function() { console.log('without delay!'); })
setTimeout(function () { console.log('with delay'); }, 1000);
var start = Date.now();
while (Date.now() < start + 3000) {} // block for 3 seconds
console.log('After wait');

according to the premise above, the output would be:

After wait
with delay
without delay

however, the output is (tested in chrome and firefox):

After wait
without delay
with delay

(note that when calling setTimeout(fn,1000), fn will be executed as soon as the interpreter will be idle after 1 second has been elapsed, this could be 1 second, 5 seconds, or even forever if the interpreter remains busy.)

the previous example led me to deduce that there is no difference between setTimeout(fn), setTimeout(fn,0), and setTimeout(fn,4) (4ms is the minimum delay in HTML5), because the function without delay got executed before the function with a delay of 1 second which has been elapsed.(the two are ready for execution)

so my question is whether they are equivalent are not? (in HTML5).

Upvotes: 0

Views: 352

Answers (4)

Guffa
Guffa

Reputation: 700262

What you get is exactly as expected.

The moment when the browser has nothing to do is not when there are no timeouts waiting at all, it's when it's not currently running anything.

After your code has blocked for three seconds and exit your code, the browser is free to start handling events, and that includes timeouts. As both timeouts have expired, both will be executed as soon as possible, but as the first timeout was first to expire, it will be the first in the queue of events to handle.

According to Mozilla, modern browsers all have a minimum delay of 4 ms:

"4ms is specified by the HTML5 spec and is consistent across browsers released in 2010 and onward"

Ref: https://developer.mozilla.org/en-US/docs/Web/API/Window.setTimeout#Minimum.2F_maximum_delay_and_timeout_nesting

Older browsers may have a higher minimum delay, for example 10 ms.

The standard specifies that the default value of the timeout parameter is zero, which then of course are adjusted to the minimum delay.

Ref: http://www.w3.org/TR/2011/WD-html5-20110525/timers.html#get-the-timeout

So, calling setTimeout without a timeout, or with a timeout of 0 or 4, are all equivalent. Even in older browsers they will all have the same result, even if the minimum timeout is higher.

Upvotes: 1

Alex Shilman
Alex Shilman

Reputation: 1547

According to mozilla, There is a minimum that is set by every browser if you dont assign the delay. So if the minimum of your browser is 4ms, then 0 and 4 would mean the same.

successive setTimeout() calls with delay smaller than the "minimum delay" limit are forced to use at least the minimum delay.

Upvotes: 0

Rob W
Rob W

Reputation: 348992

No timeout is equivalent to 0, and 0 is equivalent to 4, according to the W3 specification for timers:

http://www.w3.org/TR/2011/WD-html5-20110525/timers.html#dom-windowtimers-settimeout:

...
4. Get the timeout, and let timeout be the result.
5. 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.

http://www.w3.org/TR/2011/WD-html5-20110525/timers.html#get-the-timeout:

When the above methods are to get the timeout, they must run the following steps:

  1. Let timeout be the second argument to the method, or zero if the argument was omitted.

  2. Apply the ToString() abstract operation to timeout, and let timeout be the result. [ECMA262]

  3. Apply the ToNumber() abstract operation to timeout, and let timeout be the result. [ECMA262]

  4. If timeout is an Infinity value, a Not-a-Number (NaN) value, or negative, let timeout be zero.

  5. Round timeout down to the nearest integer, and let timeout be the result.

  6. Return timeout.

Upvotes: 1

Bic
Bic

Reputation: 3134

They would be functionally equivalent. If you put a 0ms delay in a SetTimeout, it will still wait 4ms.

Note, the interval for the SetTimeout will not start counting until the current execution cycle runs to completion. This is why 'without delay' is being printed first.

Upvotes: 0

Related Questions