javascript - What is the difference between setTimeout(fn, 0) and setTimeout(fn, 1)? - Stack Overflow

The jQuery source features uses of setTimeout with both 0 and 1 as second argument. I'm under the

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?

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?

Share Improve this question edited Oct 13, 2020 at 16:26 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Dec 1, 2011 at 13:00 RandomblueRandomblue 116k150 gold badges362 silver badges556 bronze badges 4
  • 25 1 ms difference... – Leon Commented Dec 1, 2011 at 13:01
  • 2 @Leon: I was under the impression that browsers cannot execute with that level of precision, bumping up the setTimeout time to a minimum of 13 milliseconds. – Randomblue Commented Dec 1, 2011 at 13:03
  • never heard about that magical 13, but here's a quote: "setTimeout in most browsers doesn't allow a delay less than about 10 milliseconds (it forces any smaller delays to be longer)" – Leon Commented Dec 1, 2011 at 13:10
  • @Leon, You would think that 1MS would be the difference, but that depends on if there are other items in the event loop. – Adam Fowler Commented Apr 21, 2017 at 21:47
Add a comment  | 

6 Answers 6

Reset to default 30

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."

I think the answer is "It depends" now.

We can run the code in different platform 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();
}

  1. For Node.js, 0 is converted to 1, so they are exactly the same: https://github.com/nodejs/node/blob/master/lib/timers.js#L319, and result might 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
    
  2. For Chrome, the result is quite similar with Node.js

  3. For Firefox, most of 0 will be printed before 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
    

I'm not sure the given answers are correct. Running the following code in Chrome, 0 is clearly invoking the bound 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 be doing something like Node.js's setImmediate, pushing an instruction onto the end of the current call stack, while 1 invokes whatever the implementation regards as a minimum value.

Programmatically and computationally there is a difference, but it is not a difference you will see when you execute it, as it is only 1 ms.

I would imagine that if the timeout is set to 1 ms, it pauses that script and allows other scripts to run meanwhile. And as you probably know, JavaScript is singlethreaded, so that might be your reason right there.


Thanks to molf who corrected my thoughts. It would seem that setting it to  ms is merely a trick to get it to run in the next tick of the event loop.

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

In essence, it means that this method is not very urgent to execute compared to other browser tasks like page rendering. Moreover, the JavaScript code will run after the waiting tasks are over.

Practical wise, there is no difference between using 0 or 1. This is just programmer's choice. Ideally the number chosen by coders is below 4 which may be due to the reason pointed out by Amaan.

BTW, for basic information on JavaScript timers, refer to http://ejohn.org/blog/how-javascript-timers-work/

It's simply an example of bad code practice in the jQuery source.

That's all. There's no reason to favor 0 over 1 or vice versa.

Raise a jQuery bug, have it fixed / normalized to use one or the other.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1737249137a3928712.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信