Agyan
adda
Run
Reset
Copy
JavaScript Tutorial › JS Timers › Countdown timer
Back to tutorial
Code
Edit, then press Run or Ctrl+Enter
<h1 id="time">10</h1> <button id="start">Start</button> <button id="stop">Stop</button> <script> let seconds = 10; let timer = null; const show = () => document.getElementById('time').textContent = seconds; document.getElementById('start').onclick = () => { if (timer) return; timer = setInterval(() => { seconds--; show(); if (seconds === 0) { clearInterval(timer); timer = null; document.getElementById('time').textContent = 'Time up!'; } }, 1000); }; document.getElementById('stop').onclick = () => { clearInterval(timer); timer = null; }; </script>
Result