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:
HTML code:
<div id="myTime"></div>
<input type="button" value="start Interval" onclick="startInterval();"/>
<input type="button" value="stop Interval" onclick="stopInterval();"/>
JavaScript code:
<script language="javascript">
var interval; function startInterval()
{
interval = setInterval("startTime();",1000);
}
function startTime()
document.getElementById('myTime').innerHTML = Date();
function stopInterval()
clearInterval(interval);
</script>
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.
Be the first to rate this post
Tags: javascript, javascript setinterval, javascript clearinterval, javascript delay, javascript use, javascript tutorials, learn javascript, javascript examples
10/11/2008 3:42:33 AM