I want to start counter when I click the button first time and stop when I click on second time.
I tried the following code:
var myTimer;
function clock(){
myTimer = setInterval(myClock,1000);
var c = 0;
function myClock(){
document.getElementById('demo').innerHTML = ++c;
}
}
<h1>---Counter----</h1>
<p id="demo"></p>
<button onclick="clock();clearInterval(myTimer);">start</button>
I want to start the counter after the initial click of button and stop it in the second click.