clearInterval
Stops a repeating setInterval.
Available since: Web API
Syntax
clearInterval(intervalId)Parameters
| Parameter | Type | Description |
|---|---|---|
| intervalId | number | The id returned by setInterval. |
Return value
undefinedReturns nothing.
Examples
Stopping after 3 runs:
let ticks = 0;
const id = setInterval(() => {
ticks++;
console.log('tick', ticks);
if (ticks === 3) clearInterval(id);
}, 100);Output:
tick 1
tick 2
tick 3Notes
Save the id in a variable when starting — without it the interval cannot be stopped.
See also
- setInterval — Runs a function again and again at an interval.
- clearTimeout — Cancels a scheduled setTimeout.
Related recipes
In the tutorial
- Timers — The setTimeout and setInterval functions in JavaScript: delayed and repeated calls, arguments, cancelling a timer.