-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
183 lines (157 loc) · 5.48 KB
/
script.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
//get html element by class ID
const startBtn = document.getElementById("start");
const pauseBtn= document.getElementById("pause");
const resetBtn = document.getElementById("reset");
const container = document.getElementById("container");
const bell = document.querySelector("audio");
let intervalMins = document.getElementById("interval-mins");
let intervalSecs = document.getElementById("interval-secs");
let roundCounter = document.getElementById("count");
let shortBreakMins = document.getElementById("short-break-mins");
let shortBreakSecs = document.getElementById("short-break-secs");
let longBreakMins = document.getElementById("long-break-mins")
let longBreakSecs = document.getElementById("long-break-secs")
let setIntervalMins = document.getElementById("interval-input");
let setShortBreakMins = document.getElementById("short-break-input");
let setLongBreakMins = document.getElementById("long-break-input");
const interval = document.getElementById("interval-timer");
// addEventListener() on save button
document.querySelector('form').addEventListener('submit', (e)=>{
e.preventDefault();
localStorage.setItem("interval-input", setIntervalMins.value);
localStorage.setItem("short-break-input", setShortBreakMins.value);
localStorage.setItem("long-break-input", setLongBreakMins.value);
intervalMins.innerText = setIntervalMins.value;
shortBreakMins.innerText = setShortBreakMins.value;
longBreakMins.innerText = setLongBreakMins.value;
});
// initialize timers
let roundTimer, shortBreakTimer, longBreakTimer;
// function to setInterval() for interval timer
function startRoundTimer() {
roundTimer = setInterval( beginRound, 1000);
}
// function to start interval timer
function beginRound(){
document.title = intervalMins.innerText + ":" + intervalSecs.innerText;
if (intervalSecs.innerText != 0){
intervalSecs.innerText--;
}
else if (intervalMins.innerText != 0 && intervalSecs.innerText == 0){
intervalSecs.innerText = 59;
intervalMins.innerText--;
}
else if(intervalMins.innerText == 00 && intervalSecs.innerText == 00 ){
container.style.border = "5px solid #e63946";
bell.play();
roundCounter.innerText++;
stopRoundTimer();
if(roundCounter.innerText == 4){
startLongTimer();
}
else {
startShortTimer();
}
}
}
// function to clear interval timer
function stopRoundTimer() {
// container.style.border = "5px solid rgb(69, 123, 157)";
clearInterval(roundTimer);
intervalMins.innerText = setIntervalMins.value;
intervalSecs.innerText = '00';
}
// function to setInterval() for short break timer
function startShortTimer() {
shortBreakTimer = setInterval(beginShort,1000);
}
// function to start short break timer
function beginShort(){
document.title = shortBreakMins.innerText + ":" + shortBreakSecs.innerText;
if(shortBreakSecs.innerText != 00){
shortBreakSecs.innerText--
}
else if (shortBreakMins.innerText != 00 && shortBreakSecs.innerText == 00){
shortBreakSecs.innerText = 59;
shortBreakMins.innerText--;
}
else if (shortBreakMins.innerText == 00 && shortBreakSecs.innerText == 00){
bell.play();
stopShortTimer();
startRoundTimer();
}
}
// function to clear short break timer
function stopShortTimer() {
clearInterval(shortBreakTimer);
shortBreakMins.innerText = setShortBreakMins.value;
shortBreakSecs.innerText = '00';
}
// function to setInterval() for long break timer
function startLongTimer() {
longBreakTimer = setInterval( beginLong, 1000)
}
// function to start long break timer
function beginLong(){
document.title = longBreakMins.innerText + ":" + longBreakSecs.innerText;
if(longBreakSecs.innerText != 00){
longBreakSecs.innerText--;
}
else if (longBreakMins.innerText != 00 && longBreakSecs.innerText == 00){
longBreakSecs.innerText = 59;
longBreakMins.innerText--;
}
else if (longBreakMins.innerText == 00 && longBreakSecs.innerText == 00){
bell.play();
stopLongTimer();
startRoundTimer();
}
}
// function to clear long break timer
function stopLongTimer() {
roundCounter.innerText = 00;
clearInterval(longBreakTimer);
longBreakMins.innerText = setLongBreakMins.value;
longBreakSecs.innerText = '00';
}
// addEventListener() on start button
startBtn.addEventListener('click', function(){
if (!roundTimer){
startRoundTimer();
}
else {
alert('Timer is already running. ≧◠‿◠≦ ');
}
});
// addEventListener() on pause button
pauseBtn.addEventListener('click', function(){
if(!roundTimer){
alert('Cannot pause as timer is not running. ≧◠‿◠≦ ');
return;
}
else if(roundTimer){
clearInterval(roundTimer);
roundTimer = false;
}
});
// addEventListener() on reset button
resetBtn.addEventListener('click', function(){
if(!roundTimer){
alert('Cannot reset as timer is not running. ≧◠‿◠≦ ');
return;
}
else if(roundTimer){
let confirmQuestion = confirm("Are you sure you want to reset the Pomodoro Timer?")
if(confirmQuestion == true){
if (roundTimer){
stopRoundTimer();
stopShortTimer();
stopLongTimer();
roundTimer = false;
}
}
else {
return;
}
}
});