-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (25 loc) · 936 Bytes
/
index.js
File metadata and controls
29 lines (25 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const timerElement = document.querySelector(".timer"); // Use ".timer" to select by class
const startButton = document.getElementById("start");
const stopButton = document.getElementById("stop");
let intervalId;
let currentTime = 0;
function formatTime(seconds) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
const secs = seconds % 60;
return `${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}:${secs.toString().padStart(2, "0")}`;
}
function updateTimer() {
timerElement.textContent = formatTime(currentTime);
currentTime++;
}
startButton.addEventListener("click", () => {
clearInterval(intervalId);
intervalId = setInterval(updateTimer, 1000);
updateTimer();
});
stopButton.addEventListener("click", () => {
clearInterval(intervalId);
currentTime = 0;
timerElement.textContent = formatTime(currentTime);
});