Usage
The clearInterval() is used to clear interval which is mentioned in setinterval method.Example: If setinterval is used to display message at every particular time, then we can use clearinterval to stop display that particular message.
|
Syntax
clearInterval(function);
|
<button onclick="javascript:startIt();">Start</button>
<button onclick="javascript:stopIt();">Stop</button>
<input type="text" id="txt_current_time" size="50" />
var timer = null;
function doIt(){
document.getElementById("txt_current_time").value = (new Date());
}
function startIt(){
if (timer === null)
timer = setInterval(doIt,1000);
}
function stopIt(){
clearInterval(timer);
timer = null;
}
|
Output
It will display Start and Stop button.
Once you click start button, it will display time at every particular time interval in text box.
Sat Dec 06 2014 12:06:02 GMT+0530 (India Standard Time)
Once you click stop button, it will clear set values and stop the function.
|