Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between setTimeout(fn, 0) and setTimeout(fn, 1)?

The jQuery source features uses of setTimeout with both 0 and 1 as second argument. I'm under the impression that they both mean "execute the function as soon as you can".

Is this correct? Is there a difference between the two?

like image 497
Randomblue Avatar asked Dec 01 '11 13:12

Randomblue


People also ask

What is the difference between setTimeout FN 0 vs setImmediate FN )?

setTimeout(,0) essentially means execute after all current functions in the present queue get executed. No guarantees can be made about how long it could take. setImmediate is similar in this regard except that it doesn't use queue of functions. It checks queue of I/O eventhandlers.

What does setTimeout 0 do?

setTimeout(callback, 0) executes the callback with a delay of 0 milliseconds.

What is the difference between $timeout and setTimeout?

setTimeout in order to display an alert message after a timeout of at least 2000 milliseconds. Angular $timeout is a wrapper written for window. setTimeout in form of a try catch block which throws exceptions via $exceptionHandler service. $timeout accepts the function to be delayed, delay time, a boolean to invoke $.

Why is the setTimeout () function used?

setTimeout() The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.


1 Answers

setTimeout has a minimum timeout of 4ms. So there is actually no difference between the two.

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.

Spec

EDIT: As pointed out by Ahmad in the comments, the spec has changed now, so the answer would currently be, "It depends."

like image 90
Some Guy Avatar answered Sep 19 '22 23:09

Some Guy