javascript 有没有办法查看变量是否分配了超时?

发布于 2024-12-27 13:50:17 字数 269 浏览 3 评论 0原文

我目前正在程序中使用一个每隔几毫秒重复一次的循环。在此循环中,它检查某个输入,如果收到,则取消另一个超时。本质上:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妳是的陽光 2025-01-03 13:50:17

clearTimeout() 的语法是:

clearTimeout(timerID2);

它是一个接受 setTimeout() 返回的 ID 的函数;即您不会在返回的 ID 上调用它。

如果您传递给它的值不是有效的 ID,clearTimeout 不会出错。

有关详细信息,请参阅 MDC 上的clearTimeout() 文档< /a>.

The syntax for clearTimeout() is;

clearTimeout(timerID2);

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.

鱼忆七猫命九 2025-01-03 13:50:17

请使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文