-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
209 lines (184 loc) · 5.86 KB
/
script.js
File metadata and controls
209 lines (184 loc) · 5.86 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
207
208
209
"use strict";
// Select elements
const favicon = document.querySelector("link[rel*='icon']");
const selectionContainer = document.querySelector(".timer-btns-container");
const btnPomodoro = document.querySelector(".pomodoro");
const timerBtns = document.querySelectorAll(".btn-timer");
const btnShortBreak = document.querySelector(".short-break");
const btnLongBreak = document.querySelector(".long-break");
const timerMinutes = document.querySelector(".minutes");
const timerSeconds = document.querySelector(".seconds");
const btnStartTimer = document.querySelector(".btn-start");
const progressBar = document.querySelector(".progressbar");
const quoteText = document.querySelector(".quote-text");
const quoteAuthor = document.querySelector(".quote-author");
const year = document.querySelector(".year");
// Set initial time
let time = 25 * 60;
let progressBarFill = 0;
let activeTimer = "work"; // break
// Declare timer in global scope
let timer;
// load audio files
const audioSwitchOn = new Audio("audio/switch-on.mp3");
const audioSwitchOff = new Audio("audio/switch-off.mp3");
const audioAlarm = new Audio("audio/alarm.mp3");
// Set current year
year.textContent = new Date().getFullYear() + " |";
// Display quotes dynamically
const quotes = [
["– Mahatma Gandhi", "The future depends on what you do today."],
[
"– Paul J. Meyer",
"Productivity is never an accident. It is always the result of a commitment to excellence, intelligent planning, and focused effort.",
],
[
"– Steve Jobs",
"Your time is limited, so don’t waste it living someone else’s life.",
],
[
"– Robert Collier",
"Success is the sum of small efforts, repeated day-in and day-out.",
],
["– Sam Levenson", "Don’t watch the clock; do what it does. Keep going."],
["– Tim Ferriss", "Focus on being productive instead of busy."],
[
"– Walt Disney",
"The way to get started is to quit talking and begin doing.",
],
[
"– Zig Ziglar",
"You don’t have to be great to start, but you have to start to be great.",
],
["– Unknown", "It’s not about having time. It’s about making time."],
[
"– Stephen Covey",
"The key is not to prioritize what's on your schedule, but to schedule your priorities.",
],
];
const displayQuotes = function () {
const quoteId = Math.trunc(Math.random() * quotes.length);
[quoteAuthor.textContent, quoteText.textContent] = quotes[quoteId];
};
setInterval(displayQuotes, 5000);
// Read time from clock
const getDisplayedTime = function () {
return (
Number(timerMinutes.textContent) * 60 + Number(timerSeconds.textContent)
);
};
// Change bg color, time, progress bar etc.
const changeTimer = function (btn, bgColor, minute, seconds) {
timerBtns.forEach((btn) => btn.classList.remove("btn-clicked"));
btn.classList.add("btn-clicked");
document.body.style.backgroundImage = bgColor;
// progressBar.style.width = "0%";
timerMinutes.textContent = String(minute).padStart(2, 0);
timerSeconds.textContent = String(seconds).padStart(2, 0);
time = minute * 60 + seconds;
progressBarFill = 0;
activeTimer = btn.dataset.type;
};
// Starting the timer
const startTimer = function () {
const fill = 100 / time;
document.title = "Timer started!";
const tick = function () {
time--;
let minute = String(Math.trunc(time / 60));
let second = String(time % 60);
timerMinutes.textContent = `${minute.padStart(2, "0")}`;
timerSeconds.textContent = `${second.padStart(2, "0")}`;
fillProgressBar(fill);
if (time === 0) {
clearInterval(timer);
switchTimer();
audioAlarm.play();
}
};
// reduce time by 1 at first
tick();
const timer = setInterval(tick, 1000);
return timer;
};
// progress bar
const fillProgressBar = function (fill) {
progressBarFill += fill;
progressBar.style.width = `${progressBarFill}%`;
};
// Change start button state to 'pause' or 'start'
const changebtnState = function () {
if (btnStartTimer.classList.contains("started")) {
btnStartTimer.textContent = "Pause";
} else {
btnStartTimer.textContent = "Start";
}
};
// Pause the timer
const pauseTimer = function () {
if (timer) {
clearInterval(timer);
time = getDisplayedTime();
}
};
const switchTimer = function () {
if (activeTimer === "work") {
btnShortBreak.click();
document.title = "Take a break!";
favicon.attributes.href.nodeValue = "icon-break.svg";
} else {
progressBar.style.width = "100%";
btnPomodoro.click();
document.title = "Time to focus!";
favicon.attributes.href.nodeValue = "icon-pomodoro.svg";
}
};
// Event listeners
selectionContainer.addEventListener("click", function (e) {
// Close guard
if (!e.target.classList.contains("btn-timer")) return;
// Clear timer if already started
if (timer) clearInterval(timer);
// Change start button state
btnStartTimer.classList.remove("started");
changebtnState();
if (e.target.classList.contains("pomodoro")) {
changeTimer(
e.target,
`linear-gradient(135deg, #ff9d6c 10%, #bb4e75 100%)`,
25,
0
);
} else if (e.target.classList.contains("short-break")) {
changeTimer(
e.target,
`linear-gradient(135deg, #79f1a4 10%, #0e5cad 100%)`,
5,
0
);
} else if (e.target.classList.contains("long-break")) {
changeTimer(
e.target,
`linear-gradient(135deg, #52e5e7 10%, #130cb7 100%)`,
15,
0
);
}
});
btnStartTimer.addEventListener("click", function (e) {
// Check condition to pause timer
if (e.target.classList.contains("started")) {
audioSwitchOff.play();
pauseTimer();
} else {
time = getDisplayedTime(); // 10;
audioSwitchOn.play();
timer = startTimer();
audioAlarm.pause();
audioAlarm.currentTime = 0;
}
// Add a class to pause timer when clicked again
e.target.classList.toggle("started");
// Change start button text
changebtnState();
});