clearInterval(interval)
Some interval timers do not run forever, but need to be stopped at some time (maybe like a countdown, see the first example). clearInterval() uses the value returned by the setInterval(function, milliseconds) function.
Examples
Example: Countdown
var countdown = 10;
textLabel("countdown", countdown);
var i = setInterval(function() {
countdown = countdown - 1;
setText("countdown", countdown);
if(countdown === 0) {
clearInterval(i);
}
}, 1000);
console.log("Interval timer ID: " + i);
Example: Stop the Presses!
Run a half second interval timer until a button is pressed.
// Run a half second interval timer until a button is pressed.
button("stop", "Stop the timer");
var i = setInterval(function() {
write("Timer code ran!");
}, 500);
onEvent("stop", "click", function(){
clearInterval(i);
});
Syntax
clearInterval(interval);
Parameters
Name | Type | Required? | Description |
---|---|---|---|
interval | number | The value returned by the setInterval function that you want clear. |
Returns
Tips
Tips
- If you want to clear an interval you need to save the value returned by setInterval, var i = setInterval(callback, ms);
Found a bug in the documentation? Let us know at support@code.org.