-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
107 lines (99 loc) · 3.17 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
let currentPhase = 'prepare';
let timer;
let totalTime;
let prepareTime, workTime, restTime, cycles, tabatas;
let isPaused = false;
let isFinished = false;
function startResumeTimer() {
if (!timer) {
prepareTime = parseInt(document.getElementById('prepareTime').value);
workTime = parseInt(document.getElementById('workTime').value);
restTime = parseInt(document.getElementById('restTime').value);
cycles = parseInt(document.getElementById('cycles').value);
tabatas = parseInt(document.getElementById('tabatas').value);
totalTime = prepareTime;
currentPhase = 'prepare';
document.body.style.backgroundColor = 'lightgreen';
timer = setInterval(timerFunction, 1000);
} else if (isPaused) {
isPaused = false;
timer = setInterval(timerFunction, 1000);
}
}
function stopTimer() {
clearInterval(timer);
timer = null;
isPaused = true;
}
function playTimer() {
if (isPaused) {
isPaused = false;
timer = setInterval(timerFunction, 1000);
}
}
function resetTimer() {
stopTimer();
document.getElementById('prepareTime').value = 10;
document.getElementById('workTime').value = 20;
document.getElementById('restTime').value = 10;
document.getElementById('cycles').value = 8;
document.getElementById('tabatas').value = 1;
document.getElementById('timerDisplay').textContent = '00:00';
document.body.style.backgroundColor = '';
}
function updateDisplay(time) {
if (!isFinished) {
let minutes = Math.floor(time / 60);
let seconds = time % 60;
minutes = minutes < 10 ? '0' + minutes : minutes;
seconds = seconds < 10 ? '0' + seconds : seconds;
document.getElementById('timerDisplay').textContent = minutes + ':' + seconds;
}
}
function timerFunction() {
if (totalTime <= 0) {
playSound('beepSound');
switch (currentPhase) {
case 'prepare':
totalTime = workTime;
currentPhase = 'work';
document.body.style.backgroundColor = 'red';
playSound('workSound');
break;
case 'work':
totalTime = restTime;
currentPhase = 'rest';
document.body.style.backgroundColor = 'lightblue';
playSound('restSound');
break;
case 'rest':
if (cycles > 1) {
totalTime = workTime;
currentPhase = 'work';
document.body.style.backgroundColor = 'red';
cycles--;
} else if (tabatas > 1) {
totalTime = prepareTime;
currentPhase = 'prepare';
document.body.style.backgroundColor = 'lightgreen';
tabatas--;
cycles = parseInt(document.getElementById('cycles').value);
} else {
clearInterval(timer);
timer = null;
document.body.style.backgroundColor = '';
isFinished = true;
document.getElementById('timerDisplay').textContent = 'Hurray, You Finished ' + document.getElementById('tabatas').value + ' tabatas!';
}
break;
}
}
updateDisplay(totalTime);
totalTime--;
}
function playSound(soundId) {
const sound = document.getElementById(soundId);
if (sound) {
sound.play();
}
}