-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
178 lines (147 loc) · 4.89 KB
/
app.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
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
const timesArr = ["hours", "minutes", "seconds"];
// input selectors
const hours = document.querySelector("#hours");
const minutes = document.querySelector("#minutes");
const seconds = document.querySelector("#seconds");
// default inputs value
let def_hours, def_minutes, def_seconds;
// btn selectors
const btnStart = document.querySelector(".btn_start");
const btnPause = document.querySelector(".btn_pause_resume");
const btnStop = document.querySelector(".btn_stop");
const btnReset = document.querySelector(".btn_reset");
let interval;
let isPause = false;
let message = document.querySelector(".message");
let initialTimeInSeconds;
function convertToCurrectValues(totalTimeSeconds) {
let newSeconds = totalTimeSeconds % 60;
let newMinutes = Math.floor(totalTimeSeconds / 60);
let newHours;
if (newMinutes >= 60) {
newMinutes = Math.floor(totalTimeSeconds / 60);
newHours = totalTimeSeconds % 60;
} else {
newHours = 0;
}
hours.value = newHours;
minutes.value = newMinutes;
seconds.value = newSeconds;
return true;
}
// get current Total Time by seconds
function getCurrentTotalTime() {
let hours_value = parseInt(hours.value);
let minutes_value = parseInt(minutes.value);
let seconds_value = parseInt(seconds.value);
let totalTimeSeconds =
hours_value * 60 * 60 + minutes_value * 60 + seconds_value;
convertToCurrectValues(totalTimeSeconds);
initialTimeInSeconds = totalTimeSeconds;
return totalTimeSeconds;
}
// start timer
btnStart.addEventListener("click", (event) => {
def_hours = parseInt(hours.value);
def_minutes = parseInt(minutes.value);
def_seconds = parseInt(seconds.value);
totalTimeSeconds = getCurrentTotalTime();
if (totalTimeSeconds === 0) {
message.innerHTML = "<div class='red-text'>Time is not valid</div>";
setTimeout(function () {
message.innerHTML = "";
}, 2000);
return false;
}
event.target.style.display = "none";
btnPause.style.display = "block";
btnPause.disabled = false;
btnStop.style.display = "block";
btnReset.style.display = "block";
isPause = false;
startTimer(totalTimeSeconds);
});
// set countdown timer
function startTimer(totalTimeSeconds) {
interval = setInterval(() => {
if (totalTimeSeconds > 0 && !isPause) {
handleProgressBar(totalTimeSeconds);
totalTimeSeconds--;
updateTimeInputs(totalTimeSeconds);
} else if (totalTimeSeconds == 0) {
clearInterval(interval);
// btnPause.disabled = true;
document.querySelectorAll(".btn").forEach((btn) => {
btn.style.display = "none";
});
btnStart.style.display = "block";
resetProgressBar();
document.querySelectorAll(".time_input").forEach((input) => {
input.disabled = false;
});
}
}, 1000);
}
// update inputs value
function updateTimeInputs(totalTimeSeconds) {
let hours_updated = Math.floor(totalTimeSeconds / 3600);
let minutes_updated = Math.floor(Math.floor(totalTimeSeconds % 3600) / 60);
let seconds_updated = Math.floor(Math.floor(totalTimeSeconds % 3600) % 60);
timesArr.forEach((element) => {
eval(element).disabled = true;
eval(element).value = eval(element + "_updated");
});
}
// pause event handler
btnPause.addEventListener("click", (event) => {
isPause = !isPause;
if (isPause) {
event.target.textContent = "Resume";
} else {
event.target.textContent = "Pause";
}
});
// stop event handler
btnStop.addEventListener("click", () => {
btnStart.style.display = "block";
btnPause.style.display = "none";
btnPause.disabled = false;
btnStop.style.display = "none";
btnReset.style.display = "none";
isPause = true;
hours.value = def_hours;
minutes.value = def_minutes;
seconds.value = def_seconds;
timesArr.forEach((element) => {
eval(element).disabled = false;
});
handleProgressBar(getCurrentTotalTime());
clearInterval(interval);
});
// reset event handler
btnReset.addEventListener("click", () => {
clearInterval(interval);
btnPause.disabled = false;
isPause = false;
hours.value = def_hours;
minutes.value = def_minutes;
seconds.value = def_seconds;
totalTimeSeconds = getCurrentTotalTime();
startTimer(totalTimeSeconds);
});
function handleProgressBar(totalTimeSeconds) {
let progressBarWidth = (totalTimeSeconds * 100) / initialTimeInSeconds;
if (progressBarWidth < 80 && progressBarWidth >= 30) {
document.querySelector(".progressbar").classList.add("middle");
} else if (progressBarWidth < 30) {
document.querySelector(".progressbar").classList.remove("middle");
document.querySelector(".progressbar").classList.add("low");
} else {
document.querySelector(".progressbar").classList.remove("middle", "low");
}
document.querySelector(".progressbar").style.width = `${progressBarWidth}%`;
}
function resetProgressBar() {
document.querySelector(".progressbar").classList.remove("middle", "low");
document.querySelector(".progressbar").style.width = "100%";
}