0
Hello, I wonder if it is possible to stop a setinterval after a certain time, if possible, as?
0
Hello, I wonder if it is possible to stop a setinterval after a certain time, if possible, as?
3
It is possible, you will need to work with the clearInterval function.
let qtd = 0;
let interval = setInterval( () => {
console.log("Entrando...");
qtd++;
if (qtd == 5) {
clearInterval(interval);
}
}, 1000 );
https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval
2
Only use clearInterval()
Example:
var myVar = setInterval(myTimer, 1000);
function myTimer() {
var d = new Date();
var t = d.toLocaleTimeString();
document.getElementById("demo").innerHTML = t;
}
function myStopFunction() {
clearInterval(myVar);
}
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.