function setUp(){
// Save the h3 element
var element = document.getElementById("initial");
// Create setInterval, save to variable to clear later
var timer = setInterval(function() {
// Current element value
var value = element.innerHTML;
// Decrease
value--;
// Set value
element.innerHTML = value;
// Clear if we're at 0
if (value === 0) {
clearTimeout(timer);
}
}, 1000);
}
<button onclick="setUp()">Activate timer</button>
<h3 id="initial">60</h3>
Hello, Thanks for your code.
- I want to add a "Stop timer" Button to this code. How do I do that?
- I also want the countdown to start from 60 seconds when I click the "Activate timer" Button again?
Please help. Thanks in advance.