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?
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.
setTimeout(callback, 0) executes the callback with a delay of 0 milliseconds.
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 $.
setTimeout() The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.
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."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With