clearTimeout(timeout)
Category:Control
Sometimes you need to clear a timeout timer before it executes. clearTimeout() uses the value returned by the setTimeout(function, milliseconds) function.
Examples
Example: clear timeout
// Timeout is cleared immediately.
var t = setTimeout(function() {
console.log("The timeout has completed");
}, 10000);
console.log("Timer ID: " + t);
clearTimeout(t);
Example: Stop the Countdown
Example: Stop the Countdown The user controls whether to clear the 3 second timeout timer.
// The user controls whether to clear the 3 second timeout timer.
textLabel("instructions", "Click Start to begin a 3 second timeout timer, then Stop to prevent it from completing");
textLabel("status", "");
button("startButton", "Start");
button("cancelButton", "Stop");
var t;
onEvent("startButton", "click", function(){
t = setTimeout(function() {
setText("status", "The timer completed!");
}, 3000);
setText("status", "Timer started!");
console.log("Timer ID: " + t);
});
onEvent("cancelButton", "click", function(){
if(t) {
clearTimeout(t);
setText("status", "The timer has been cancelled.");
} else {
setText("status", "You need to start the timer before you can stop it :)");
}
});
Syntax
clearTimeout(timeout);
Parameters
Name | Type | Required? | Description |
---|---|---|---|
timeout | number | The value returned by the setTimeout function to cancel. |
Returns
No return value.
Tips
Tips
- If you want to clear a timeout interval you need to save the value returned by setTimeout, var i = setTimeout(callback, ms);
Found a bug in the documentation? Let us know at support@code.org.