简体   繁体   English

setTimeout 回调执行后清除超时

[英]clear timeout after the setTimeout callback execution

Does this clear my timeout这是否清除了我的超时

  const timer1 = setTimeout(()=> {
            setSuccessMessage('')
            clearTimeout(timer1)
        },3000);

The code execute well, but I need to know if the timer is cleared at the end.代码执行得很好,但我需要知道计时器最后是否被清除。 I've tested a solution given in this post but the timer get cleared before my stuff get finished, and considering that my timeout is intended to hide a message, the former remains displayed because the timer got cleared.我已经测试了这篇文章中给出的解决方案,但是计时器在我的东西完成之前被清除了,并且考虑到我的超时是为了隐藏一条消息,前者仍然显示,因为计时器被清除了。

You do not need to cancel your timer here as it is automatically garbage collected after it is executed.您无需在此处取消您的计时器,因为它在执行后会自动进行垃圾回收。

You do need to cancel it only if you ever intend to cancel it BEFORE it is fired, which is not your case.只有当您打算在它被解雇之前取消它时,您才需要取消它,这不是您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM