-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
206 lines (178 loc) · 6.32 KB
/
Copy pathmain.js
File metadata and controls
206 lines (178 loc) · 6.32 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
let timerDecrementTime = 0;
let timerIncrementTime = 0;
let timer;
let timerActive = false;
let timerPaused = false;
let displayMs = true;
let controller = new AbortController();
window.addEventListener("load", () => {
const dropdown = document.querySelector("#dropdown");
dropdown.value = "incremental";
dropdown.addEventListener("change",() => {
const decrementalOptions = document.querySelector("#decrementalOptions");
stopTimer();
if (isIncrementalTimer()) {
decrementalOptions.classList.add("hidden");
} else {
decrementalOptions.classList.remove("hidden");
}
});
const checkbox = document.querySelector("#msCheckbox");
checkbox.addEventListener("click", () => {
displayMs = checkbox.checked;
if (!timerActive) {
setTimerText(0);
}
});
const themeDropdown = document.querySelector("#theme");
themeDropdown.addEventListener("change", setStyle);
const funCheckbox = document.querySelector("#funCheckbox");
funCheckbox.addEventListener("change", () => {
if (funCheckbox.checked) {
document.body.classList.add('fun');
} else {
document.body.classList.remove('fun');
}
})
setStyle();
addInputListeners();
});
function setStyle() {
const themeDropdown = document.querySelector("#theme");
const themeStylesheet = document.querySelector("#themeSheet");
switch (themeDropdown.value) {
case "light":
themeStylesheet.href = "simpleLight.css";
break;
case "dark":
themeStylesheet.href = "simpleDark.css";
break;
case "green":
themeStylesheet.href = "greenscreen.css";
break;
case "purple":
themeStylesheet.href = "purpleGradient.css";
break;
}
}
function addInputListeners() {
const hourInput = document.querySelector("#hourInput");
hourInput.addEventListener("change", () => highlightRedIfBad(hourInput));
const minuteInput = document.querySelector("#minuteInput");
minuteInput.addEventListener("change", () => highlightRedIfBad(minuteInput));
const secondInput = document.querySelector("#secondInput");
secondInput.addEventListener("change", () => highlightRedIfBad(secondInput));
const msInput = document.querySelector("#msInput");
msInput.addEventListener("change", () => highlightRedIfBad(msInput));
}
function setDecrementTime(ms) {
if (timerActive) {
return;
}
timerDecrementTime = ms;
setTimerText(ms);
}
function startTimer() {
if (timerActive && !timerPaused) {
return;
}
controller = new AbortController();
timerActive = true;
timerPaused = false;
const startTime = Date.now() - timerIncrementTime;
const pauseButton = document.querySelector("#pause");
if (isIncrementalTimer()) {
timer = setInterval(() => {
setTimerText(Date.now() - startTime);
}, 1000/60)
pauseButton.addEventListener("click", () => {
timerPaused = true;
timerIncrementTime = Date.now() - startTime;
clearInterval(timer);
}, {once: true,
signal: controller.signal});
} else {
let ms = timerDecrementTime;
const endTime = new Date(startTime + ms).valueOf();
timer = setInterval(() => {
ms = endTime - Date.now();
if (ms <= 0) {
stopTimer();
if (document.querySelector("#audioCheckbox").checked) {
new Audio('250629__kwahmah_02__alarm1.mp3').play();
}
return;
}
setTimerText(ms);
}, 1000 / 60);
pauseButton.addEventListener("click", function decrementPauseListener() {
timerPaused = true;
timerDecrementTime = ms;
clearInterval(timer);
}, {once: true,
signal: controller.signal})
}
}
function stopTimer() {
clearInterval(timer);
controller.abort();
setTimerText(0);
timerActive = false;
timerPaused = false;
timerIncrementTime = 0;
timerDecrementTime = 0;
}
function manualTimeSubmit() {
const hours = document.querySelector("#hourInput");
const minutes = document.querySelector("#minuteInput");
const seconds = document.querySelector("#secondInput");
const ms = document.querySelector("#msInput");
if (inputIsValid(hours) && inputIsValid(minutes) && inputIsValid(seconds) && inputIsValid(ms))
setDecrementTime(3600000 * parseInt(hours.value) + 60000 * parseInt(minutes.value) + 1000 * parseInt(seconds.value) + parseInt(ms.value));
else
alert("Invalid value(s)!");
}
function isIncrementalTimer() {
return document.querySelector("#dropdown").value === "incremental";
}
function highlightRedIfBad(element) {
if (element.value === "")
element.value = 0;
if (inputIsValid(element)) {
element.classList.remove("redBorder");
} else {
element.classList.add("redBorder");
}
}
function inputIsValid(inputElement) {
const elementValue = parseInt(inputElement.value);
const elementMax = parseInt(inputElement.max);
const elementMin = parseInt(inputElement.min);
return !(isNaN(inputElement.value) || elementValue > elementMax || elementValue < elementMin)
}
function getTime(ms) {
const map = new Map();
map.set("hours", Math.floor(ms / 3600000));
let excessMs = ms % 3600000;
map.set("minutes", Math.floor(excessMs / 60000));
excessMs %= 60000
map.set("seconds", Math.floor(excessMs / 1000));
map.set("ms", excessMs % 1000);
return map;
}
function setTimerText(ms) {
const textElement = document.querySelector("#timerText");
const time = getTime(ms);
if (displayMs) {
textElement.innerText =
`${time.get("hours").toString().padStart(2, "0")}:` +
`${time.get("minutes").toString().padStart(2, "0")}:` +
`${time.get("seconds").toString().padStart(2, "0")}.` +
`${time.get("ms").toString().padStart(3, "0")}`;
} else {
textElement.innerText =
`${time.get("hours").toString().padStart(2, "0")}:` +
`${time.get("minutes").toString().padStart(2, "0")}:` +
`${time.get("seconds").toString().padStart(2, "0")}`;
}
}