Difference between setTimeout (fn, 0) and setTimeout (fn, 1)?

jquery source uses setTimeout functions with two 0 and 1 as second argument. I get the impression that they both mean "execute the function as soon as you can."

It is right? Is there a difference between the two?

+24
javascript jquery
Dec 01 '11 at 13:00
source share
6 answers

I think the answer is "Now it depends."

We can run code in different platforms and browsers:

 function setTimeouts() { setTimeout(function() { console.log(2); }, 2); setTimeout(function() { console.log(1); }, 1); setTimeout(function() { console.log(0); }, 0); } for (var i = 0; i < 10; i++) { setTimeouts(); } 
  • For Node.js, 0 converts to 1 , so they are exactly the same: https://github.com/nodejs/node/blob/master/lib/timers.js#L319 , and the result could be:

     1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 2 2 2 2 2 2 2 2 2 2 
  • For Chrome, the result is very similar to Node.js

  • For firefox, most of 0 will print to 1 :

     0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 
+9
Jun 22 '16 at 7:53 on
source share

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

If the currently running task is a task created by the setTimeout () method and the timeout is less than 4, increase the timeout to 4.

Spec

EDIT: As Ahmad noted in the comments, the specification has changed now, so the answer will be: "It depends."

+27
Dec 01 '11 at 13:07 on
source share

I am not sure if these answers are correct. When executing the following code in Chrome, 0 explicitly calls the binding function more quickly (just switch the timer values โ€‹โ€‹between 0 and 1 ):

 console.log("A"); console.log("B"); var start = new Date().getTime(); setTimeout(function() { console.log(new Date().getTime() - start); }, 0); console.log("C"); console.log("D"); 

0 seems to do something like NodeJS setImmediate , pushing the instruction at the end of the current call stack, and 1 calls everything that the implementation considers as the minimum value.

+3
Apr 24 '13 at 3:29
source share

There is a difference programmatically and computationally, but this is not the difference that you will see when it is executed, since it is only 1ms .

I would suggest that if the timeout is set to 1ms , it pauses this script and allows other scripts to work at the same time. And, as you probably know, javascript is one-way, so that might be your reason right there.

EDIT:

Thanks to @molf, who corrected my thoughts, it would seem that setting it to 0ms is just a trick to get it working in the next tick of the event loop.

+2
Dec 01 2018-11-12T00:
source share

For reasons why setTimeout (fn, 0) or setTimeout (fn, 1) is necessary, check Why is setTimeout (fn, 0) sometimes useful?

In essence, this means that this method is not very urgent to perform compared to other browser tasks, such as rendering pages. In addition, the js code will work after completion of the wait tasks. Practically reasonable, there is no difference between using 0 or 1. This is just the choice of the programmer. Ideally, the number chosen by the encoders is less than 4, which may be due to the reason noted by Amaan.

btw, for basic Javascript timers, see http://ejohn.org/blog/how-javascript-timers-work/

+1
Dec 01 2018-11-12T00:
source share

This is just an example of bad code practice in a jQuery source.

It's all. There is no reason to approve 0 over 1 or vice versa.

Raise a jQuery bug, fix / normalize it to use one or the other.

-one
Dec 01 '11 at 13:16
source share



All Articles