javascript 有没有办法查看变量是否分配了超时?
我目前正在程序中使用一个每隔几毫秒重复一次的循环。在此循环中,它检查某个输入,如果收到,则取消另一个超时。本质上:
if (inputreceived && secondTimerRunning)
{
timerID2.clearTimeout();
secondTimerRunning = false;
}
但是,这些行导致我的循环终止。据我所知,这是因为我试图清除不存在的超时。有没有办法防止这种情况,或者我使用超时错误?
I'm currently using a loop that repeats itself every few milliseconds in my program. In this loop, it checks for a certain input, and if received, cancels another timeout. Essentially:
if (inputreceived && secondTimerRunning)
{
timerID2.clearTimeout();
secondTimerRunning = false;
}
However, those lines cause my loop to terminate. From what I can tell, it's because I am trying to clear a Timeout that doesn't exist. Is there a way to prevent this, or am I using Timeouts wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
clearTimeout()
的语法是:它是一个接受 setTimeout() 返回的 ID 的函数;即您不会在返回的 ID 上调用它。
如果您传递给它的值不是有效的 ID,
clearTimeout
不会出错。有关详细信息,请参阅 MDC 上的
clearTimeout()
文档< /a>.The syntax for
clearTimeout()
is;It is a function which accepts the ID returned by
setTimeout()
; i.e. you don't call it on the ID returned.clearTimeout
will not error if the value you pass to it is not a valid ID.For more info, see the documentation for
clearTimeout()
on MDC.请使用
clearTimeout(timerID2)
代替,超时由数字标识符表示,clearTimeout 是可以直接调用的全局函数。Use
clearTimeout(timerID2)
instead, timeouts are represented by a number identifier and clearTimeout is a global function that can be called directly.