-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
229 lines (195 loc) · 7.13 KB
/
timer.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
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
//Return button elements from the document
const focusButton = document.querySelector(".focus");
const shortBreakButton = document.querySelector(".break-short");
const longBreakButton = document.querySelector(".break-long");
const resetButton = document.querySelector(".reset");
//Global Variables
let sound = new Audio("birds-voice.mp3");
let roundCounter = 0; //Focus counter
let countShortbreaks = 0; //Short break counter
let minutesInterval;
let secondsInterval;
let minutes;
let seconds;
("use strict");
/* FOCUS EVENT ---------------------------------------------------------------------------------------------------------- */
function startFocus() {
//Reset
clearIntervals();
minutes = "24";
seconds = "59";
document.querySelector(".minutes").textContent = minutes;
document.querySelector(".seconds").textContent = seconds;
minutesInterval = setInterval(
minutesTimer,
60000
); /* 1minute is equal to 60000 milliseconds */
secondsInterval = setInterval(secondsTimer, 1000);
//Reset the display message
document.querySelector(".message").textContent = "";
document
.querySelector(".message")
.classList.remove("show-message"); /*to make an element visible*/
//Minutes timer when user clicks on FOCUS button
function minutesTimer() {
minutes = ("0" + (minutes - 1)).slice(-2);
document.querySelector(".minutes").textContent = minutes;
}
function secondsTimer() {
seconds = ("0" + (seconds - 1)).slice(-2);
document.querySelector(".seconds").textContent = seconds;
/*to prevent infinity*/
if (seconds <= 0) {
if (minutes <= 0) {
/* stop interval */
clearInterval(minutesInterval);
clearInterval(secondsInterval);
document.querySelector(".message").textContent = "Take a short break";
document
.querySelector(".message")
.classList.add("show-message"); /* to make an element visible */
sound.play();
roundCounter++;
updateCount();
}
seconds = 60;
}
}
}
focusButton.addEventListener("click", startFocus);
//Set daily target of 12 sets for every 25 min of interval.
const targetData = document.querySelector(".target");
const updateCount = () => {
const target = +targetData.getAttribute("data-target"); //convert data type from string to number by adding + sign to the front of the variable targetData
const count = roundCounter;
if (count < target) {
targetData.textContent = roundCounter;
setTimeout(updateCount, 1); // update the count after 1 milisecond.
} else {
targetData.textContent = target;
document.querySelector(".message").textContent =
"Welldone! You Hit the Max Level of Productivity!";
document.querySelector(".message").style.backgroundColor = "#20bf6b";
}
};
/* SHORT BREAK EVENT -------------------------------------------------------------------------------------------------------------- */
function shortBreak() {
//Reset
clearIntervals();
minutes = "05";
seconds = "59";
document.querySelector(".minutes").textContent = minutes;
document.querySelector(".seconds").textContent = seconds;
minutes = ("0" + (minutes - 1)).slice(-2);
document.querySelector(".minutes").textContent = minutes;
minutesInterval = setInterval(
minutesTimer,
60000
); /* 1minute is equal to 60000 milliseconds */
secondsInterval = setInterval(secondsTimer, 1000);
//Reset the display message
document.querySelector(".message").textContent = "";
document
.querySelector(".message")
.classList.remove("show-message"); /*to make an element visible*/
// Minutes timer when user clicks on 'SHORT BREAK' button
function minutesTimer() {
minutes = ("0" + (minutes - 1)).slice(-2);
document.querySelector(".minutes").textContent = minutes;
}
function secondsTimer() {
seconds = ("0" + (seconds - 1)).slice(-2);
document.querySelector(".seconds").textContent = seconds;
/*to prevent infinity*/
if (seconds <= 0) {
if (minutes <= 0) {
/* stop interval */
clearInterval(minutesInterval);
clearInterval(secondsInterval);
//After the third round, display message directing to LONG BREAK.
countShortbreaks++;
if (countShortbreaks === 3) {
document.querySelector(".message").textContent =
"Time for Long Break!";
document
.querySelector(".message")
.classList.add("show-message"); /*to make an element visible*/
} else {
document.querySelector(".message").textContent = "Time to Focus!";
document
.querySelector(".message")
.classList.add("show-message"); /*to make an element visible*/
}
sound.play();
}
seconds = 60;
}
}
}
shortBreakButton.addEventListener("click", shortBreak);
/* LONG BREAK EVENT -------------------------------------------------------------------------------------------------------------- */
function longBreak() {
//Reset
clearIntervals();
minutes = "15";
seconds = "59";
document.querySelector(".minutes").textContent = minutes;
document.querySelector(".seconds").textContent = seconds;
minutes = ("0" + (minutes - 1)).slice(-2);
document.querySelector(".minutes").textContent = minutes;
minutesInterval = setInterval(
minutesTimer,
60000
); /* 1minute is equal to 60000 milliseconds */
secondsInterval = setInterval(secondsTimer, 1000);
//Reset the display message
document.querySelector(".message").textContent = "";
document
.querySelector(".message")
.classList.remove("show-message"); /* to make an element visible */
// Minutes timer when user clicks on 'SHORT BREAK' button
function minutesTimer() {
minutes = ("0" + (minutes - 1)).slice(-2);
document.querySelector(".minutes").textContent = minutes;
}
function secondsTimer() {
seconds = ("0" + (seconds - 1)).slice(-2);
document.querySelector(".seconds").textContent = seconds;
/*to prevent infinity*/
if (seconds <= 0) {
if (minutes <= 0) {
/* stop interval */
clearInterval(minutesInterval);
clearInterval(secondsInterval);
document.querySelector(".message").textContent = "Time to Focus!";
document
.querySelector(".message")
.classList.add("show-message"); /* to make an element visible */
sound.play();
}
seconds = 60;
}
}
}
longBreakButton.addEventListener("click", longBreak);
/* RESET EVENT -------------------------------------------------------------------------------------------------------------- */
function resetTimer() {
minutes = "00";
seconds = "00";
clearIntervals();
document.querySelector(".minutes").textContent = minutes;
document.querySelector(".seconds").textContent = seconds;
}
function clearIntervals() {
clearInterval(minutesInterval);
clearInterval(secondsInterval);
}
resetButton.addEventListener("click", resetTimer);
function clickPrimaryButton(e) {
let target = e.target;
console.log(target);
console.log("you clicked the secondary button");
}
//to do:
//add fade out feature
//display - You better not play with buttons! - if user clicks to another button during the session!