JavaScript clearInterval function clears the instance of timer calls of setInterval function. clearInterval stops the timer function of setInterval that calls the specified function after specified time intervals in milliseconds.
Javascript clearInterval syntax
clearInterval( timerID );
clearInterval takes 1 parameter:
timerID: timer id of the timer instance initiated by setInterval function.
setInterval method returns the timerID of the instance initiated by it.
Example of JavaScript clearInterval with setInterval function
var interval;
function startInterval()
{
interval = setInterval("startTime();",1000);
}
function startTime()
{
document.getElementById('myTime').innerHTML = Date();
}
function stopInterval()
{
clearInterval(interval);
}
In the above example of javascript clearInterval and setInterval 1 variable (interval) is declared as global to access its value in any function at any time when required. interval a global variable is used to store the timerID returned by setInterval function. clearTimeout function takes this interval variable as a timerID parameter that cancels the timer set by setInterval function.
Continue to next tutorial: Javascript Clock Timer to learn how to create a JavaScript Clock timer.