Stopwatch with Start, Stop and Reset in JavaScript
Build a browser UI stopwatch in JavaScript with Start, Stop and Reset buttons and performance.now. Save elapsed time on stop; repeated Start clicks are ignored.
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</button><button id="stop">Stop</button><button id="reset">Reset</button><p id="time">0.0 s</p>7<script>8let startedAt = null;9let saved = 0;10const time = document.querySelector('#time');11function elapsed() { return saved + (startedAt === null ? 0 : performance.now() - startedAt); }12function render() { time.textContent = (elapsed() / 1000).toFixed(1) + ' s'; }13document.querySelector('#start').onclick = () => {14 if (startedAt === null) startedAt = performance.now();15};16document.querySelector('#stop').onclick = () => {17 if (startedAt !== null) { saved = elapsed(); startedAt = null; }18 render();19};20document.querySelector('#reset').onclick = () => { saved = 0; startedAt = null; render(); };21setInterval(render, 100);22</script>23</body>24</html>
Sample Output
About 2.0 s after Start and Stop
The clock difference is divided by 1000 and rounded for display. The exact reading depends on your clicks.
0.0 s after Reset
Reset clears the saved duration and stops the active run.
In this one
- Measure duration with performance.now
- Keep running and stopped states separate
- Pause and resume a duration
- Avoid counting delayed timer ticks
How it works
- 1
Start with saved zero and no active start time.
- 2
On Start, record a clock reading only if stopped.
- 3
Compute elapsed time from the clock difference plus saved time.
- 4
On Stop, save that total and clear the active start time.
- 5
Observe approximate seconds, resume them, then reset to 0.0 s.
Edge cases
Start clicked twice
A second click should not lose the current run.
The null check prevents replacing startedAt while already running.
Hidden tab
The display interval may be delayed.
Elapsed time comes from the clock difference, so it does not depend on every redraw happening on time.
What's going on
This browser UI project measures elapsed time with Start, Stop and Reset buttons. This page can time a short revision exercise, pause it, resume it, and reset it. Its display is an example of changing page output rather than a fixed console answer.
performance.now gives a clock reading in milliseconds that is useful for measuring durations. saved holds time from earlier runs, and startedAt is null while stopped. While running, elapsed adds the saved time to the difference between the current clock and the latest start.
The interval only redraws the display. It does not add a guessed 100 milliseconds on each callback, because browsers can delay callbacks in busy or hidden tabs. Stop saves the current duration once and returns to the stopped state. Repeated Start clicks do not reset the running clock. Reset stops the watch as well as clearing its saved time. The exact displayed value depends on when you click.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does the interval do?
Pick an answer to check it.
Question 2
What does Reset do here?
Pick an answer to check it.
Questions people ask
Why is startedAt null instead of zero?
null clearly means stopped. A clock value of zero could be a valid reading.
Does Stop clear the time?
No. It saves the elapsed duration so Start can continue. Reset clears it.
Is the display exact to one tenth of a second?
It is rounded to one decimal place and refreshed periodically. The click timing and browser scheduling still affect what you see.