javascript setTimeout(fn, 0) 和 setTimeout(fn, 1) 的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8341803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:07:18  来源:igfitidea点击:

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

javascriptjquery

提问by Randomblue

The jquery sourcefeatures uses of setTimeoutwith both 0and 1as second argument. I'm under the impression that they both mean "execute the function as soon as you can".

所述的jquery源设有的用途setTimeout与两个01作为第二个参数。我的印象是它们都意味着“尽快执行该功能”。

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

这个对吗?两者有区别吗?

采纳答案by Freewind

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, 0is 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 0will 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
    
  1. 对于 Node.js,0转换为1,因此它们完全相同:https://github.com/nodejs/node/blob/master/lib/timers.js#L319,结果可能是:

    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. 对于 Chrome,结果与 Node.js 非常相似

  3. 对于 Firefox,大部分0将在之前打印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
    

回答by Some Guy

setTimeouthas a minimum timeout of 4ms. So there is actually nodifference between the two.

setTimeout最小超时时间为 4 毫秒。所以两者实际上没有区别。

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.

如果当前运行的任务是 setTimeout() 方法创建的任务,并且 timeout 小于 4,则将 timeout 增加到 4。

Spec

规格

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

编辑:正如艾哈迈德在评论中指出的那样,规范现在已经改变,所以目前的答案是,“这取决于。”

回答by Sandro Pasquali

I'm not sure the given answers are correct. Running the following code in Chrome, 0is clearly invoking the bound function more quickly (just switch the timer values between 0and 1):

我不确定给出的答案是否正确。在 Chrome 中运行以下代码,0显然可以更快地调用绑定函数(只需在0和之间切换计时器值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");

0seems to be doing something like NodeJS's setImmediate, pushing an instruction onto the end of the current call stack, while 1invokes whatever the implementation regards as a minimum value.

0似乎在做类似 NodeJS 的事情setImmediate,将指令推送到当前调用堆栈的末尾,同时1调用任何实现视为最小值的内容。

回答by Jan Dragsbaek

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

以编程方式和计算方式存在差异,但您在执行它时不会看到差异,因为它只是1ms.

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

我想如果超时设置为1ms,它会暂停该脚本并允许其他脚本同时运行。正如您可能知道的那样,javascript 是单线程的,所以这可能就是您的原因。

EDIT:

编辑:

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

感谢@molf 纠正了我的想法,似乎将其设置为 0ms 只是让它在事件循环的下一个滴答声中运行的一个技巧。

回答by sv_in

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

由于需要 setTimeout(fn, 0) 或 setTimeout(fn, 1) 的原因,请查看为什么 setTimeout(fn, 0) 有时有用?

In essence it means that this method is not very urgent to execute compared to other browser tasks like page rendering. Moreover, the js 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.

从本质上讲,这意味着与其他浏览器任务(如页面渲染)相比,此方法执行起来不是很紧迫。而且,js代码会在等待任务结束后运行。实际操作中,使用 0 或 1 没有区别。这只是程序员的选择。理想情况下,编码员选择的数字低于 4,这可能是由于 Amaan 指出的原因。

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

顺便说一句,有关 Javascript 计时器的基本信息,请参阅http://ejohn.org/blog/how-javascript-timers-work/

回答by Raynos

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

这只是 jQuery 源代码中不良代码实践的一个示例。

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

就这样。我们没有理由偏爱0超过1或正相反。

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

提出一个 jQuery 错误,将其修复/规范化以使用其中一个。