Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

clearInterval

Stops a repeating setInterval.

Available since: Web API

Syntax

clearInterval(intervalId)

Parameters

ParameterTypeDescription
intervalIdnumberThe 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 3

Notes

Save the id in a variable when starting — without it the interval cannot be stopped.

See also

Related recipes

In the tutorial

  • Timers — The setTimeout and setInterval functions in JavaScript: delayed and repeated calls, arguments, cancelling a timer.

Thanks for reading!

Donate to the site — we work hard for you.