简体   繁体   English

清除间隔(); 给定 setInterval 的 ID 不起作用

[英]clearInterval(); doesn't work given the setInterval's ID

First off, I don't want the function to start running as soon as I open the file, secondly I can't stop the interval...首先,我不希望该功能在打开文件后立即开始运行,其次我无法停止间隔...

I've tried the setInterval(sing, 1000) and get a number 7 but I can't stop the code by doing clearInterval(7)我已经尝试了setInterval(sing, 1000)并得到了一个数字 7 但我无法通过执行clearInterval(7)来停止代码

function sing() {
    console.log("twinke twinke...");
    console.log("adadasdsad...");
}

setInterval(sing, 1000);

The ID to stop the interval is the result of "setInterval".停止间隔的 ID 是“setInterval”的结果。 For example:例如:

// starting setInterval
const id = setInterval(() => {
 // do something...
}, 1000);

// to stop the interval
clearInterval(id);

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

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