Category: Control
Clears an existing interval timer by passing in the numeric value returned by setInterval().
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.
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); });
clearInterval(interval);
Name | Type | Required? | Description |
---|---|---|---|
interval | number | Yes | The value returned by the setInterval function that you want clear. |
No return value.
Found a bug in the documentation? Let us know at documentation@code.org
Found a bug in the documentation? Let us know at documentation@code.org