setTimeout与0ms之后是否需要clearTimeout? [英] Is clearTimeout necessary after setTimeout with 0ms?

查看:146
本文介绍了setTimeout与0ms之后是否需要clearTimeout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我已经了解到的(这里: https://www.youtube.com/watch? v = 8aGhZQkoFbQ )在某些情况下调用延迟0ms的setTimeout会很有用(因为事件循环)。

As i have already learned (here: https://www.youtube.com/watch?v=8aGhZQkoFbQ) it can be useful in some cases to call a setTimeout with 0ms delay (because of the event loop).

现在每当我使用时 setTimeout 我还要注意在适当的位置调用 clearTimeout ,以确保没有任何东西留在某处并在某个时刻执行我不希望它被执行。

Now ususally whenever I use setTimeout I also take care to call clearTimeout at the appropriate spot to make sure nothing remains somewhere and gets executed at a point where I do not want it to be executed.

所以我的问题是:是否有必要(有意义)调用 clearTimeout 在0ms之后的 setTimeout 之后?传递的函数会立即附加到回调队列中,因此我假设 clearTimeout 不会(也不能)执行任何操作。或者 clearTimeout 甚至从回调队列中删除传递的函数(所以在超时到期之后但在函数执行之前)?

So my question is: Is it necessary (does it make sense) to call clearTimeout after a setTimeout with 0ms? The passed function is immediately appended to the callback queue so I would assume clearTimeout does not (and cannot) do anything. Or can clearTimeout remove the passed function even from the callback queue (so after the timeout expired but before the function has been executed)?

第二个问题:即使它没有做任何事情,无论如何,总是在这些情况下调用 clearTimeout 是最佳做法吗?

Second question: Even if it does not do anything, is it anyway 'best practice' to call clearTimeout always in those cases?

推荐答案


在具有0ms的setTimeout之后调用clearTimeout是否有必要(有意义)吗?

Is it necessary (does it make sense) to call clearTimeout after a setTimeout with 0ms?

从跑步。执行顺序可以完全根据调用调用回调来讨论。

It is necessary if the goal is to prevent the asynchronous timer callback from running. The execution ordering can be discussed entirely in terms of when the callback is invoked.

首先,0毫秒的值作为延迟意味着'尽快运行回调'(来自未来的异步上下文),但是

First off, the value of 0 milliseconds as a delay means 'run the callback as soon as possible' (from an future asynchronous context), but:


  1. 更改 setTimeout如何工作 ;和

0




传递的函数会立即附加到回调队列中,因此我认为clearTimeout不会(也不能)执行任何操作。

The passed function is immediately appended to the callback queue so I would assume clearTimeout does not (and cannot) do anything.

这是不正确的。传递的函数 not 立即附加到回调队列。相反,超时到期时,计时器仍处于活动状态,将调用回调函数。可能有其他异步回调 - 来自定时器或其他 - 可以在之前运行。

This is incorrect. The passed function is not "immediately appended to the callback queue". Rather, when the timeout expires and the timer is still active, the callback function will be invoked. There may be other asynchronous callbacks - from timers or otherwise - that could be run prior.

此外,所有剩余的同步代码保证在定时器回调发生之前运行:清除此上下文中的超时可防止定时器回调被调用,无论同步代码所用的时间。

Also, all remaining synchronous code is guaranteed to run before the timer callback occurs: clearing the timeout in this context prevents the timer callback from ever being called, irrespective of the time taken in the synchronous code.


即使它没有做任何事情,在这种情况下总是调用clearTimeout是最佳做法吗?

Even if it does not do anything, is it anyway 'best practice' to call clearTimeout always in those cases?

呼叫clearTimeout将

Calling clearTimeout will either


  1. 阻止回调执行,如果在回调之前清除(因为它删除了定时器),或者;

  1. prevent the callback from executing, if cleared prior to the callback (as it removes the timer), or;

如果回调已经发生,则不执行任何操作(因为计时器不再有效)

do nothing if the callback has already occurred (as the timer is no longer active)

因此,为了正确运行代码/算法,需要清除定时器;或者这是一个无用的操作。创建一个计时器只是为了立即取消它可能毫无意义,但这是关于代码结构的一个题外话。

Thus clearing the timer is required for correct functioning of the code/algorithm; or it is a useless operation. Creating a timer only to immediately cancel it may be pointless, but that's a digression about code structure..


我也注意调用clearTimeout在适当的地方,以确保没有任何东西留在某处,并在我不希望它被执行的地方执行。

I also take care to call clearTimeout at the appropriate spot to make sure nothing remains somewhere and gets executed at a point where I do not want it to be executed.

As如上所述,无需手动清除不再有效的计时器;并且取消定时器之前来调用定时器回调将删除定时器,从而阻止定时器回调执行。

As per above, there is no need to manually clear a timer that is no longer active; and cancelling a timer prior to the invocation of the timer callback will remove the timer and thus prevent the timer callback from executing.

当/何时/如果取消定时器取决于整个设计。

When/where/if a timer should be cancelled depends on the design as a whole.

取消执行代码中的超时会阻止来自运行的回调:A或B回调都不会运行。

Cancelling the timeout in the executing code prevents the callback from running: neither the "A" or "B" callback will run.

a = setTimeout(function () { console.log("A"); }, 0);
clearTimeout(a);

b = setTimeout(function () { console.log("B"); }, 0);
s = Date.now()
while (Date.now() - s < 100) { /* waste 100ms of CPU */ }
clearTimeout(b);

在首先运行的异步事件中取消超时会阻止回调运行:B回调永远不会运行:。

Cancelling the timeout in an asynchronous event that runs first prevents the callback from running: the "B" callback will never run:.

a = setTimeout(function () { console.log("A"); clearTimeout(b); }, 0);
b = setTimeout(function () { console.log("B"); }, 0);

虽然使用了辅助计时器(因为订购得到了很好的保证),但是有可能是其他的异步事件(按钮点击,Web工作者,AJAX等)也可能在0ms超时之前发生。

While a secondary timer is used (as the ordering is well guaranteed), there is a chance that other asynchronous events (button clicks, web workers, AJAX, etc.) could also occur before a "0ms" timeout.

在回调后调用clearTimeout(来自任何上下文)没用:

A clearTimeout invoked after the callback (from any context) is useless:

a = setTimeout(function () { console.log("A"); clearTimeout(a); }, 0);

a = setTimeout(function () { console.log("A"); }, 0);
b = setTimeout(function () { console.log("B"); clearTimeout(a); }, 0);

这篇关于setTimeout与0ms之后是否需要clearTimeout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆