Countdown Timer with a Start Button in JavaScript
Make a browser UI countdown in JavaScript with a start button and a three-second deadline demo. Subtract Date.now on each redraw; restarting clears the old timer.
Save this as index.html and open it in a browser.
1<!doctype html>2<html lang="en">3<meta charset="utf-8">4<title>JavaScript practice</title>5<body>6<button id="start">Start 3-second countdown</button><p id="left">Ready</p>7<script>8let interval = null;9const output = document.querySelector('#left');10document.querySelector('#start').onclick = () => {11 if (interval !== null) clearInterval(interval);12 const deadline = Date.now() + 3000;13 function render() {14 const remaining = Math.max(0, deadline - Date.now());15 output.textContent = remaining === 0 ? 'Time is up!' : Math.ceil(remaining / 1000) + (Math.ceil(remaining / 1000) === 1 ? ' second left' : ' seconds left');16 if (remaining === 0) { clearInterval(interval); interval = null; }17 }18 interval = setInterval(render, 100);19 render();20};21</script>22</body>23</html>
Sample Output
3, 2, then 1 second left
Ceiling rounds a positive remaining fraction upward. Scheduling delays can skip a displayed number.
Time is up!
The clock reached or passed the deadline, and the interval was cleared.
In this one
- Create a future deadline
- Convert milliseconds into displayed seconds
- Stop a completed interval
- Restart without duplicate timers
How it works
- 1
Clear a previous countdown before starting another.
- 2
Set the deadline to the current time plus 3000 ms.
- 3
Recalculate remaining time on each redraw.
- 4
Round positive fractions upward and clamp negative values to zero.
- 5
At zero, show Time is up and clear the interval; delayed redraws can skip numbers.
Edge cases
Repeated Start
Two intervals would otherwise update the same output.
Clear the existing interval before creating another one.
Tab sleeps past the deadline
A background tab may not show every second.
Recalculate from the deadline when the callback runs again. Do not promise an alarm at an exact instant.
What's going on
This browser UI project uses a start button to run a three-second deadline demo. A short practice-question timer is a good example. Unlike a stopwatch, it moves toward zero instead of increasing from zero.
The button creates a deadline three seconds after the current time. Each redraw subtracts Date.now from that deadline. Math.max keeps the remaining time from going below zero, and Math.ceil rounds up so a fraction of a second still appears as one second left.
The interval checks the clock every 100 milliseconds, but the browser is allowed to delay it. That is why the program uses a deadline instead of subtracting one on every tick. Reaching zero clears the interval and shows Time is up. Clicking Start again clears the old interval and creates a fresh deadline, so two countdowns do not compete for the same paragraph. This lesson uses the computer's calendar clock. Changing that clock can affect it.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does Date.now return?
Pick an answer to check it.
Question 2
Why clear the old interval?
Pick an answer to check it.
Questions people ask
Why use Math.ceil?
With 0.2 seconds remaining, it shows 1 instead of 0. The final message appears only when time has actually run out.
Will it keep counting below zero?
No. Math.max clamps the remainder to zero, and the interval is cleared.
How do I make it ten seconds?
Change 3000 to 10000. The deadline calculation uses milliseconds.