forked from akshitagupta15june/Moksh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reward.js
86 lines (76 loc) · 2.32 KB
/
reward.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
const timer = document.querySelector('.time-left');
const startButton = document.querySelector('.start');
const resetButton = document.querySelector('.reset');
let defaultTime = 25 * 60;
const breakTime = 5 * 60;
let cycleCount = 0;
let countdown;
function timerDisplay(seconds) {
const minutes = Math.floor(seconds / 60);
const remainderSeconds = seconds % 60;
const display = `${minutes}:${remainderSeconds < 10 ? '0' : ''}${remainderSeconds}`;
timer.textContent = display;
document.title = display;
}
function startTimer() {
console.log('start button clicked!!!');
//display pause instead of start
startButton.textContent = 'Pause';
startButton.removeEventListener('click', startTimer);
timerDisplay(defaultTime);
countdown = setInterval(() => {
defaultTime <= 0 ? clearInterval(countdown) : defaultTime--;
timerDisplay(defaultTime);
if (defaultTime === 0) {
clearInterval(countdown);
playSpiritualSound();
if (cycleCount < 3) {
cycleCount++;
startBreakTimer();
} else {
cycleCount = 0;
defaultTime = 25 * 60;
timerDisplay(defaultTime);
alert('Congratulations! You have completed four cycles.');
}
}
}, 1000);
startButton.addEventListener('click', pauseTimer);
}
function playSpiritualSound()
{
console.log('Playing spiritual sound');
const audio = new Audio("sounds/bell-sound-timer-end.mp3")
audio.play()
}
function pauseTimer() {
console.log('pause button clicked!!!');
startButton.textContent = 'Start';
startButton.removeEventListener('click', pauseTimer);
clearInterval(countdown);
startButton.addEventListener('click', startTimer);
}
function startBreakTimer() {
console.log('break timer started!!!');
defaultTime = breakTime;
timerDisplay(defaultTime);
countdown = setInterval(() => {
defaultTime <= 0 ? clearInterval(countdown) : defaultTime--;
timerDisplay(defaultTime);
if (defaultTime === 0) {
clearInterval(countdown);
defaultTime = 1 * 1;
startTimer();
}
}, 1000);
}
function resetTimer() {
console.log('reset clicked!!!!');
startButton.textContent = 'Start';
defaultTime = 1 * 1;
cycleCount = 0;
clearInterval(countdown);
timerDisplay(defaultTime);
}
startButton.addEventListener('click', startTimer);
resetButton.addEventListener('click', resetTimer);