<button class="btn js-start">Start</button>
<button class="btn js-stop">Stop</button>
body {
margin: 16px;
text-align: center;
}
const startBtn = document.querySelector(".js-start");
const stopBtn = document.querySelector(".js-stop");
let timerId = null;
startBtn.addEventListener("click", () => {
timerId = setInterval(() => {
console.log(`I love async JS! ${Math.random()}`);
}, 1000);
});
stopBtn.addEventListener("click", () => {
clearInterval(timerId);
console.log(`Interval with id ${timerId} has stopped!`);
});
This Pen doesn't use any external JavaScript resources.