This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstopwatch.js
144 lines (109 loc) · 3.55 KB
/
stopwatch.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const timerElement = document.querySelector("p.js-time");
let timer = JSON.parse(localStorage.getItem("timer")) ?? 0;
let startTime = timer ? Date.now() - timer : null;
const numberFormat = new Intl.NumberFormat("en-GB", { minimumIntegerDigits: 3 })
.format;
updateTimerElement();
let buttonState = localStorage.getItem("buttonState");
const goButton = document.querySelector("button.js-go-button");
goButton.addEventListener("click", handleGoButtonClick);
let interval;
if (timer && buttonState !== "Go") handleGoButtonClick();
const addLapButton = document.querySelector("button.js-add-lap-button");
const lapList = document.querySelector("ol.js-lap-list");
addLapButton.addEventListener("click", addLap);
const resetButton = document.querySelector("button.js-reset-button");
document.body.addEventListener("keydown", (event) => {
switch (event.key) {
case "g":
if (buttonState === "Go") handleGoButtonClick();
break;
case "s":
if (buttonState === "Stop") handleGoButtonClick();
break;
case "a":
addLap();
break;
case "Backspace":
showResetConfirmation();
break;
case "r":
showResetConfirmation();
break;
case "y":
document.querySelector("button.js-yes-button") &&
(clearConfirmation() || resetTimer());
break;
case "n":
document.querySelector("button.js-no-button") && clearConfirmation();
break;
}
});
resetButton.addEventListener("click", showResetConfirmation);
const confirmationContainer = document.querySelector(
"div.js-confirmation-container",
);
function handleGoButtonClick() {
if (goButton.innerHTML === "Go (G)") {
startTime = Date.now() - timer;
interval = setInterval(updateTimerElement, 0);
goButton.innerHTML = "Stop (S)";
localStorage.setItem("buttonState", "Stop");
buttonState = "Stop";
} else {
clearInterval(interval);
interval = null;
goButton.innerHTML = "Go (G)";
localStorage.setItem("buttonState", "Go");
buttonState = "Go";
}
}
/**
* Formats a number of milliseconds into hours, minutes, seconds and milliseconds.
* @param {Number?} time The time to format. If none is specified, then the timer time will be used.
* @returns {String} The formatted time in hours, minutes, seconds and milliseconds.
*/
function formatTime(time) {
let ms = time ?? (startTime ? Date.now() - startTime : 0);
timer = ms;
localStorage.setItem("timer", JSON.stringify(timer));
let s = ms / 1000;
ms = Math.floor(ms % 1000);
let m = s / 60;
s = Math.floor(s % 60);
const h = Math.floor(m / 60);
m = Math.floor(m % 60);
return `${h ? `${h}h ` : ""}${m || h ? `${m}m ` : ""}${s || m || h ? `${s}s ` : ""}${numberFormat(ms)}ms`;
}
function updateTimerElement() {
timerElement.innerHTML = formatTime();
}
function addLap() {
lapList.innerHTML += `<li class="lap-item">${formatTime(timer)}</li>`;
}
function resetTimer() {
clearInterval(interval);
interval = null;
startTime = null;
timer = 0;
updateTimerElement();
if (buttonState === "Stop") handleGoButtonClick();
lapList.innerHTML = "";
}
function showResetConfirmation() {
confirmationContainer.innerHTML = `
<div>Are you sure you want to reset the timer?</div>
<button class="yes-button js-yes-button">Yes (Y)</button>
<button class="no-button js-no-button">No (N)</button>
`;
const noButton = document.querySelector("button.js-no-button");
noButton.addEventListener("click", clearConfirmation);
const yesButton = document.querySelector("button.js-yes-button");
yesButton.addEventListener("click", () => {
clearConfirmation();
resetTimer();
});
}
function clearConfirmation() {
confirmationContainer.innerHTML = "";
}