JavaScript Window 对象的 setTimeout() 方法中的默认超时是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18275269/
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-27 11:17:36  来源:igfitidea点击:

What is default timeout in setTimeout() method of JavaScript Window object?

javascript

提问by Yuriy Rypka

According to W3Schoolstimeout is required parameter. But it is possible to call it without timeout parameter.

根据W3Schools超时是必需的参数。但是可以在没有超时参数的情况下调用它。

function eventHandler(e) {
    var t = setTimeout(function () {
        clearTimeout(t);
        alert('Called');
    });
}

回答by Bergi

The HTML5 Timerspecsays:

HTML5Timer规范说:

5) Let timeout be the second method argument, or zero if the argument was omitted.

5) 让 timeout 是第二个方法参数,如果参数被省略,则为零。

回答by James

If you don't want to set a timeout then just pass 0

如果您不想设置超时,则只需通过 0

setTimeout(function() {
    ...
}, 0);

Alternatively you can just omit the delay parameter and 0is assumed, however, for robustness (and general readability) it's best to pass somethingin there.

或者,您可以省略延迟参数并0假设,但是,为了健壮性(和一般可读性),最好在那里传递一些东西

Note - this does notguarantee that your code will run immediately. It queues your task to run whenever the UI thread is available to process it.

注意 - 这并不能保证您的代码会立即运行。只要 UI 线程可用于处理它,它就会将您的任务排队运行。

回答by losnir

Why don't you check it out for yourself? Documentation is always welcome, however you can't beat real-world examples:

你为什么不自己检查一下?文档总是受欢迎的,但是你无法击败现实世界的例子:

var startTime = Date.now();
setTimeout(function () {
    var endTime = Date.now();
    alert("timeOut was: " + (endTime - startTime) + "ms");
});

JSFiddle: http://jsfiddle.net/losnir/PbJBA/
Have a go by playing with different timeout values.

JSFiddle:http: //jsfiddle.net/losnir/PbJBA/
试试不同的超时值。

Not passing an a timeout arguments is practically the same as passing a 0. However, as you can see, the timeout is never 0, that is because your callback function will be placed in a queue, and executed as soon as possible (given your requested timeout value).

不传递 a 超时参数实际上与传递 a 相同0。但是,如您所见, timeout 为 never 0,这是因为您的回调函数将被放入队列中,并尽快执行(给定您请求的超时值)。

It goes to about 5ms on average, and if your UI thread is doing some intensive tasks, or your CPU is busy - it can go as high as a couple hundreds of milliseconds, atrocious!

它平均达到大约 5 毫秒,如果您的 UI 线程正在执行一些密集型任务,或者您的 CPU 很忙 - 它可以高达数百毫秒,太糟糕了

回答by Raymond Chen

According to the HTML5 specification:

根据 HTML5 规范

When the above methods are to get the timeout, they must run the following steps:

  • Let timeout be the second argument to the method, or zero if the argument was omitted.
  • Apply the ToString() abstract operation to timeout, and let timeout be the result. [ECMA262]
  • Apply the ToNumber() abstract operation to timeout, and let timeout be the result. [ECMA262]
  • If timeout is an Infinity value, a Not-a-Number (NaN) value, or negative, let timeout be zero.
  • Round timeout down to the nearest integer, and let timeout be the result.
  • Return timeout.

上述方法要获取超时时间,必须运行以下步骤:

  • 让 timeout 是方法的第二个参数,如果参数被省略,则为零
  • 将 ToString() 抽象操作应用于超时,并让超时作为结果。[ECMA262]
  • 将 ToNumber() 抽象操作应用于超时,并让超时作为结果。[ECMA262]
  • 如果超时是无穷大值、非数字 (NaN) 值或负值,则让超时为零。
  • 将超时舍入到最接近的整数,并让超时作为结果。
  • 返回超时。

Note that a specified timeout of zero may not actually delay for zero milliseconds. See the specification of setTimeoutfor further details.

请注意,指定的零超时实际上可能不会延迟零毫秒。有关详细信息,请参阅的规范setTimeout

回答by Hemant_Negi

Yes you can all it without providing the time, but it will of no use

是的,您可以在不提供时间的情况下完成所有工作,但这没有用

setTimeout(
function() {
 alert("asdf");
    })

it will fire on next tick

它会在下一个滴答声中触发

yes but it will make that function Asynchronous, or Queued for later

是的,但它会使该功能异步,或稍后排队

setTimeout(function() { alert("msg1");
    })
alert("msg2");

in above code msg2 is displayed first.

在上面的代码中,首先显示 msg2。