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

后端 未结 6 1994
执念已碎
执念已碎 2020-11-28 07:05

The jQuery source features uses of setTimeout with both 0 and 1 as second argument. I\'m under the impression that they both mean &quo

相关标签:
6条回答
  • 2020-11-28 07:28

    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.

    0 讨论(0)
  • 2020-11-28 07:29

    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/

    0 讨论(0)
  • 2020-11-28 07:34

    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
      
    0 讨论(0)
  • 2020-11-28 07:40

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

    0 讨论(0)
  • 2020-11-28 07:42

    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.

    0 讨论(0)
  • 2020-11-28 07:47

    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.

    0 讨论(0)
提交回复
热议问题