Stopwatch
Watch the walkthrough
Open on InstagramExercise
Build a stopwatch with Start, Stop, and Reset controls.
What the source implements
The source prevents duplicate intervals and formats hours:minutes:seconds, without zero padding. It counts interval callbacks, so background throttling causes drift. For accurate elapsed time store timestamps and use intervals only to refresh the display.
Run and revise
Copy the entire fenced HTML into a file with the same name and open it in a browser. It includes the markup, styles, and JavaScript together. Check the console, keyboard behavior, and boundary cases. Remote APIs/assets and CDN scripts need network access.
Original repository source
The complete file below is preserved apart from line endings; the extensions above are not changes to this copied implementation.
7-stopwatch.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,400;0,700;1,100&display=swap"
rel="stylesheet"
/>
<style>
body {
min-width: 350px;
max-width: 400px;
margin: 10rem auto;
color: white;
background-color: rgb(71, 79, 87);
font-family: "Poppins", sans-serif;
font-weight: 400;
font-style: normal;
}
input {
flex-grow: 1;
padding: 0.35rem 1rem;
border: 1px solid gainsboro;
border-radius: 5px;
}
button {
padding: 0.5rem 1rem;
min-width: 8rem;
cursor: pointer;
background-color: rgb(69, 119, 211);
color: white;
font-weight: 700;
border: none;
border-radius: 5px;
transition: all 0.2s ease;
}
button:hover {
background-color: rgb(55, 102, 191);
}
button:active {
scale: 0.9;
}
button:disabled {
cursor: not-allowed;
background-color: lightgray;
}
</style>
</head>
<body>
<h2>Timer: <span id="time-container"></span> s</h2>
<button id="start-btn">Start</button>
<button id="stop-btn">Stop</button>
<button id="reset-btn">Reset</button>
</body>
<script>
let time = 0;
let timer = null;
const startBtn = document.getElementById("start-btn");
const stopBtn = document.getElementById("stop-btn");
const resetBtn = document.getElementById("reset-btn");
const timeContainer = document.getElementById("time-container");
function formatTime(seconds) {
let hours = Math.trunc(seconds / 3600);
let mins = Math.trunc((seconds % 3600) / 60);
let secs = Math.trunc((seconds % 3600) % 60);
return `${hours}:${mins}:${secs}`;
}
startBtn.addEventListener("click", () => {
if (!timer) {
timer = setInterval(() => {
time++;
renderTime();
}, 1000);
startBtn.disabled = true;
stopBtn.disabled = false;
resetBtn.disabled = false;
}
});
stopBtn.addEventListener("click", () => {
clearInterval(timer);
timer = null;
startBtn.disabled = false;
stopBtn.disabled = true;
resetBtn.disabled = false;
});
resetBtn.addEventListener("click", () => {
clearInterval(timer);
time = 0;
timer = null;
renderTime();
startBtn.disabled = false;
stopBtn.disabled = true;
resetBtn.disabled = true;
});
function renderTime() {
timeContainer.innerText = formatTime(time);
}
function init() {
renderTime();
startBtn.disabled = false;
stopBtn.disabled = true;
resetBtn.disabled = true;
}
init();
</script>
</html>