简体   繁体   中英

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)

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

setInterval(sing, 1000);

The ID to stop the interval is the result of "setInterval". For example:

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

// to stop the interval
clearInterval(id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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