-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
310 lines (268 loc) · 9.65 KB
/
index.html
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdn.tailwindcss.com"></script>
<script>
let timerStatus = "stopped"; // stopped, running, paused
let totalSeconds = 0;
let remainingSecond = 0;
let elapsedSeconds = 0;
let hours = 0;
let minutes = 0;
let seconds = 0;
let interval;
function setMinutesInInput(minutes) {
stopTimer();
let minuteInputEl = document.querySelector("#minuteInput");
minuteInputEl.value = minutes;
startTimer();
}
function startTimer() {
let hourInputEl = document.querySelector("#hourInput");
let minuteInputEl = document.querySelector("#minuteInput");
let secondInputEl = document.querySelector("#secondInput");
let pauseButtonEl = document.querySelector("#pauseButton");
let playButtonEl = document.querySelector("#playButton");
let stopButtonEl = document.querySelector("#stopButton");
let editorEl = document.querySelector("#editor");
let readOnlyEl = document.querySelector("#readOnly");
pauseButtonEl.classList.add("flex");
pauseButtonEl.classList.remove("hidden");
playButtonEl.classList.remove("flex");
playButtonEl.classList.add("hidden");
stopButtonEl.classList.add("flex");
stopButtonEl.classList.remove("hidden");
editorEl.classList.add("hidden");
editorEl.classList.remove("grid");
readOnlyEl.classList.remove("hidden");
readOnlyEl.classList.add("flex");
// Logic start here
if (timerStatus !== "paused") {
hours = Number(hourInputEl.value);
minutes = Number(minuteInputEl.value);
seconds = Number(secondInputEl.value);
totalSeconds = hours * 3600 + minutes * 60 + seconds;
remainingSecond = totalSeconds;
updateReadOnly(hours, minutes, seconds);
}
timerStatus = "running";
interval = setInterval(() => {
if (remainingSecond == 0) {
// time is up
stopTimer();
return;
}
if (elapsedSeconds < totalSeconds) elapsedSeconds++;
if (remainingSecond > 0) remainingSecond--;
let timeString = convertSecondsToTimeFormat(remainingSecond);
let timeArr = timeString.split(":"); // 01:03:53
updateReadOnly(timeArr[0], timeArr[1], timeArr[2]);
// update progress bar
updateProgressBar(elapsedSeconds, totalSeconds);
}, 1000);
}
function updateProgressBar(
elapsedSeconds = undefined,
totalSeconds = undefined
) {
let progres = document.querySelector("#progress");
if (!totalSeconds) {
progres.style.width = "0";
return;
}
let totalPercentageCompleted = (elapsedSeconds / totalSeconds) * 100;
progres.style.width = totalPercentageCompleted + "%";
}
function convertSecondsToTimeFormat(seconds) {
var date = new Date(0);
date.setSeconds(seconds); // specify value for SECONDS here
var timeString = date.toISOString().substring(11, 19);
return timeString;
}
function updateReadOnly(hours, minutes, seconds) {
let readOnlyHoursEl = document.querySelector("#readOnly .hours");
let readOnlyMinutesEl = document.querySelector("#readOnly .minutes");
let readOnlySecondsEl = document.querySelector("#readOnly .seconds");
readOnlyHoursEl.innerHTML = hours;
readOnlyMinutesEl.innerHTML = minutes;
readOnlySecondsEl.innerHTML = seconds;
}
function pauseTimer() {
timerStatus = "paused";
let pauseButtonEl = document.querySelector("#pauseButton");
let playButtonEl = document.querySelector("#playButton");
pauseButtonEl.classList.remove("flex");
pauseButtonEl.classList.add("hidden");
playButtonEl.classList.add("flex");
playButtonEl.classList.remove("hidden");
clearInterval(interval);
}
function stopTimer() {
timerStatus = "stopped";
clearInterval(interval);
updateProgressBar();
let pauseButtonEl = document.querySelector("#pauseButton");
let playButtonEl = document.querySelector("#playButton");
let stopButtonEl = document.querySelector("#stopButton");
let editorEl = document.querySelector("#editor");
let readOnlyEl = document.querySelector("#readOnly");
let hourInputEl = document.querySelector("#hourInput");
let minuteInputEl = document.querySelector("#minuteInput");
let secondInputEl = document.querySelector("#secondInput");
hourInputEl.value = "00";
minuteInputEl.value = "00";
secondInputEl.value = "00";
pauseButtonEl.classList.remove("flex");
pauseButtonEl.classList.add("hidden");
playButtonEl.classList.add("flex");
playButtonEl.classList.remove("hidden");
stopButtonEl.classList.remove("flex");
stopButtonEl.classList.add("hidden");
editorEl.classList.remove("hidden");
editorEl.classList.add("grid");
readOnlyEl.classList.add("hidden");
readOnlyEl.classList.remove("flex");
totalSeconds = 0;
remainingSecond = 0;
elapsedSeconds = 0;
hours = 0;
minutes = 0;
seconds = 0;
}
</script>
</head>
<body>
<div class="p-5 bg-orange-50 h-[100vh]">
<!-- Pre defined minutes -->
<div
class="border-green-600 border grid grid-cols-6 w-full h-20 text-blue-900 font-bold rounded-md text-2xl"
>
<div
onclick="setMinutesInInput(1)"
class="flex items-center border-r-[1px] border-green-600 justify-center hover:bg-green-300 cursor-pointer"
>
1min
</div>
<div
onclick="setMinutesInInput(5)"
class="flex items-center border-r-[1px] border-green-600 justify-center hover:bg-green-300 cursor-pointer"
>
5min
</div>
<div
onclick="setMinutesInInput(10)"
class="flex items-center border-r-[1px] border-green-600 justify-center hover:bg-green-300 cursor-pointer"
>
10min
</div>
<div
onclick="setMinutesInInput(15)"
class="flex items-center border-r-[1px] border-green-600 justify-center hover:bg-green-300 cursor-pointer"
>
15min
</div>
<div
onclick="setMinutesInInput(20)"
class="flex items-center border-r-[1px] border-green-600 justify-center hover:bg-green-300 cursor-pointer"
>
20min
</div>
<div
onclick="setMinutesInInput(30)"
class="flex items-center border-green-600 justify-center hover:bg-green-300 cursor-pointer"
>
30min
</div>
</div>
<div
class="grid grid-cols-3 justify-items-center py-8 text-2xl font-bold"
>
<div>Hours</div>
<div>Minutes</div>
<div>Seconds</div>
</div>
<div id="timer">
<div id="editor" class="grid-cols-3 text-9xl grid">
<div class="flex">
<div class="w-[97%]">
<input
id="hourInput"
type="number"
class="grow h-48 text-center w-full border-2 border-gray-400"
value="00"
/>
</div>
<div class="w-[3%]">:</div>
</div>
<div class="flex">
<div class="w-[97%]">
<input
id="minuteInput"
type="number"
class="grow h-48 text-center w-full border-2 border-gray-400"
value="00"
/>
</div>
<div class="w-[3%]">:</div>
</div>
<div class="flex">
<div class="w-full">
<input
id="secondInput"
type="number"
class="grow h-48 text-center w-full border-2 border-gray-400"
value="00"
/>
</div>
</div>
</div>
<div
id="readOnly"
class="text-9xl font-bold hidden justify-evenly border-4 border-gray-300 rounded-lg h-[240px] items-center"
>
<div class="hours">12</div>
<div>:</div>
<div class="minutes">10</div>
<div>:</div>
<div class="seconds">00</div>
</div>
</div>
<div
id="progressBar"
class="bg-gray-400 h-[40px] w-full rounded-3xl mt-10 overflow-hidden"
>
<div
id="progress"
class="bg-green-600 w-0 h-full transition-all duration-500"
></div>
</div>
<div id="actions" class="pt-16 flex gap-10">
<button
id="playButton"
onclick="startTimer()"
class="bg-green-700 text-5xl flex justify-center items-center h-32 grow text-white rounded-3xl"
>
▶
</button>
<button
id="pauseButton"
onclick="pauseTimer()"
class="bg-green-700 justify-center items-center h-32 grow text-white rounded-3xl hidden"
>
<img class="w-10" src="./pauseIcon.svg" />
</button>
<button
id="stopButton"
onclick="stopTimer()"
class="bg-gray-400 text-5xl hidden justify-center items-center h-32 grow text-white rounded-3xl"
>
✕
</button>
</div>
</div>
</body>
</html>