-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathscript.js
53 lines (47 loc) · 1.46 KB
/
script.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
let timer;
let running = false;
let hours = 0, minutes = 0, seconds = 0, milliseconds = 0;
const timeDisplay = document.getElementById("time");
const msDisplay = document.getElementById("milliseconds");
const startButton = document.getElementById("start");
const clearButton = document.getElementById("clear");
function updateDisplay() {
timeDisplay.textContent =
`${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
msDisplay.textContent = String(milliseconds).padStart(3, '0');
}
function startStop() {
if (running) {
clearInterval(timer);
startButton.textContent = "Start";
} else {
timer = setInterval(() => {
milliseconds += 10;
if (milliseconds >= 1000) {
milliseconds = 0;
seconds++;
}
if (seconds >= 60) {
seconds = 0;
minutes++;
}
if (minutes >= 60) {
minutes = 0;
hours++;
}
updateDisplay();
}, 10);
startButton.textContent = "Pause";
}
running = !running;
}
function clearTimer() {
clearInterval(timer);
running = false;
hours = minutes = seconds = milliseconds = 0;
updateDisplay();
startButton.textContent = "Start";
}
startButton.addEventListener("click", startStop);
clearButton.addEventListener("click", clearTimer);
updateDisplay();