-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
185 lines (160 loc) · 4.4 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
182
183
184
185
const SLAB = 25;
const PUTERNIC = 30;
const ZEU = 50;
const allPomodoros = [SLAB, PUTERNIC, ZEU];
const START = 0;
const PAUSE = 1;
const STOP = 2;
const NONE = 0;
const WORK = 1;
const STAY = 2;
const PAUSED = 3;
const PAUSE_TIME = 5 * 60;
const timer = document.getElementById('timer');
const currentStatusHTML = document.getElementById('status');
const topButtons = document.querySelectorAll('.time-btn .btn');
const scoreHTML = document.getElementById('score');
const breakSound = new Audio('sounds/before_break.mp3');
const learnSound = new Audio('sounds/after_break.mp3');
let selectedTime = [false, true, false]
let currentPomodoro = PUTERNIC;
let currentTime = currentPomodoro * 60;
let timerInterval = null;
let currentStatus = NONE;
let score = 0;
let totalMinutes = 0;
for(let i = 0; i < topButtons.length; i++)
{
topButtons[i].addEventListener("mouseover", function()
{
topButtons[i].classList.add('active');
})
topButtons[i].addEventListener("mouseout", function()
{
if(selectedTime[i] === false)
{
topButtons[i].classList.remove('active');
}
})
topButtons[i].addEventListener("click", function()
{
for(let j = 0; j < topButtons.length; j++)
{
selectedTime[j] = false;
topButtons[j].classList.remove('active');
}
topButtons[i].classList.add('active');
selectedTime[i] = true;
currentPomodoro = allPomodoros[i];
currentTime = currentPomodoro * 60;
if(timerInterval != 0)
clearInterval(timerInterval);
currentStatus = NONE;
updateStatus(currentStatus);
changeTime();
})
}
function changeTime()
{
timer.innerHTML = currentPomodoro + ':00';
}
function manip(status)
{
switch(status)
{
case START:
{
timerInterval = setInterval(startTime, 1000);
currentStatus = WORK;
updateStatus(currentStatus);
break;
}
case PAUSE:
{
clearInterval(timerInterval);
currentStatus = PAUSED;
updateStatus(currentStatus);
break;
}
case STOP:
{
if(timerInterval != 0)
clearInterval(timerInterval);
currentTime = currentPomodoro * 60;
timer.innerHTML = currentPomodoro + ':00';
currentStatus = NONE;
updateStatus(currentStatus);
score = 0;
scoreHTML.innerHTML = `${score} Pomodoros`;
alert(`You learned ${totalMinutes} minutes`);
break;
}
}
}
function startTime()
{
--currentTime;
timer.innerHTML = formatTimes(currentTime);
if(currentTime == -1)
{
if(currentStatus == WORK)
{
score++;
scoreHTML.innerHTML = `${score} Pomodoros`;
currentStatus = STAY;
breakSound.play();
totalMinutes += currentPomodoro;
startPauseTime();
}
else if (currentStatus == STAY)
{
currentStatus = WORK;
currentTime = currentPomodoro * 60;
learnSound.play();
updateStatus(currentStatus);
}
}
}
function startPauseTime()
{
currentTime = PAUSE_TIME;
updateStatus(currentStatus);
}
function updateStatus(status)
{
switch(status)
{
case WORK:
{
currentStatusHTML.innerHTML = 'WORK';
currentStatusHTML.style.color = '#86FF7B'
break;
}
case PAUSED:
{
currentStatusHTML.innerHTML = 'PAUSED';
currentStatusHTML.style.color = '#FF7B7B'
break;
}
case NONE:
{
currentStatusHTML.innerHTML = 'WAITING';
currentStatusHTML.style.color = 'white'
break;
}
case STAY:
{
currentStatusHTML.innerHTML = 'BREAK';
currentStatusHTML.style.color = '#F868FF'
break;
}
}
}
function formatTimes(currTime)
{
let min = 0;
let sec = 0;
min = Math.floor(currTime / 60);
sec = Math.floor(currTime - min * 60);
return `${min < 10 ? '0' : ''}${min}:${sec < 10 ? '0' : ''}${sec}`
}