-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimerWorker.js
59 lines (56 loc) · 1.63 KB
/
timerWorker.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
let intervalId;
let minutes;
let seconds;
let studyTime;
let breakTime;
let isBreak = false;
self.onmessage = function(e) {
const data = e.data;
switch(data.command) {
case 'start':
startTimer(data.minutes, data.seconds);
break;
case 'pause':
clearInterval(intervalId);
break;
case 'stop':
clearInterval(intervalId);
isBreak = false;
self.postMessage({ type: 'update', minutes: data.studyTime, seconds: 0 });
break;
case 'updateSettings':
studyTime = data.studyTime;
breakTime = data.breakTime;
if (!isBreak) {
minutes = studyTime;
seconds = 0;
}
break;
}
};
function startTimer(startMinutes, startSeconds) {
minutes = startMinutes;
seconds = startSeconds;
intervalId = setInterval(() => {
if (seconds === 0) {
if (minutes === 0) {
clearInterval(intervalId);
self.postMessage({ type: 'cycleComplete' });
isBreak = !isBreak;
if (isBreak) {
minutes = breakTime;
} else {
minutes = studyTime;
}
seconds = 0;
startTimer(minutes, seconds);
} else {
minutes--;
seconds = 59;
}
} else {
seconds--;
}
self.postMessage({ type: 'update', minutes: minutes, seconds: seconds });
}, 1000);
}