<!DOCTYPE html>
<html>
<body>
<p>Click the first button to display the current time, and the second button to stop the time.</p>
<button onclick="myFunction()">Try it</button>
<button onclick="myStopFunction()">Stop time</button>
<script type="text/javascript">
var myVar;
function myFunction()
{
myVar=setInterval(function(){myTimer()},1000);
}
function myTimer()
{
var d=new Date();
var t=d.toLocaleTimeString();
document.getElementById("demo").innerHTML=t;
}
function myStopFunction()
{
clearInterval(myVar);
}
</script>
<p id="demo"></p>
</body>
</html>