-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (106 loc) · 3.27 KB
/
app.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
const timerElement = document.querySelector('.timer');
const minutesElement = document.querySelector('.minutes');
const secondsElement = document.querySelector('.seconds');
const startButton = document.querySelector('#start');
const pauseButton = document.querySelector('#pause');
const resetButton = document.querySelector('#reset');
const tickSound = document.querySelector('#tick-sound');
const breakSound = document.querySelector('#break-sound');
const finishSound = document.querySelector('#finish-sound');
let intervalId;
let startTime;
let elapsedTime = 0;
let timeLeft = 45 * 60 * 1000; // 45 minutes in milliseconds
function updateTimer() {
const now = Date.now();
elapsedTime = now - startTime;
timeLeft = Math.max(timeLeft - elapsedTime, 0);
const minutes = Math.floor(timeLeft / 1000 / 60);
const seconds = Math.floor((timeLeft / 1000) % 60);
minutesElement.textContent = minutes.toString().padStart(2, '0');
secondsElement.textContent = seconds.toString().padStart(2, '0');
if (timeLeft <= 0) {
clearInterval(intervalId);
timerElement.classList.add('finished');
playFinishSound();
setTimeout(() => {
setInterval
timeLeft = 5 * 60 * 1000; // 5 minutes in milliseconds
if (confirm('Take (' + (timeLeft/60000) + ') Minutes a break?')) {
startTimer();
playTickSound();
} else {
pauseButton.disabled = true;
resetButton.disabled = false;
}
}, 10);
resetButton.disabled = false;;
} else {
playTickSound();
// startTimer;
}
startTime = now;
}
// Start the timer
function startTimer() {
startTime = Date.now() - elapsedTime;
intervalId = setInterval(updateTimer, 100);
startButton.disabled = true;
pauseButton.disabled = false;
resetButton.disabled = false;
}
// Pause the timer
function pauseTimer() {
clearInterval(intervalId);
elapsedTime = Date.now() - startTime;
startButton.disabled = false;
pauseButton.disabled = true;
resetButton.disabled = false;
tickSound.pause();
tickSound.currentTime = 0;
}
// Reset the timer
function resetTimer() {
clearInterval(intervalId);
elapsedTime = 0;
timeLeft = 45 * 60 * 1000;
minutesElement.textContent = '45';
secondsElement.textContent = '00';
timerElement.classList.remove('finished');
startButton.disabled = false;
pauseButton.disabled = true;
resetButton.disabled = true;
tickSound.pause();
tickSound.currentTime = 0;
finishSound.pause();
finishSound.currentTime = 0;
}
// Play the tick sound
function playTickSound() {
if (tickSound.paused) {
tickSound.play();
}
}
// Play the finish sound
function playFinishSound() {
tickSound.pause();
tickSound.currentTime = 0;
if (finishSound.paused) {
finishSound.play();
}
}
// Save the timer state to localStorage before the window is closed
window.addEventListener('beforeunload', () => {
localStorage.setItem(
'timerState',
JSON.stringify({
timeLeft,
elapsedTime,
isRunning: !!intervalId,
})
);
});
// Add event listeners to the buttons
startButton.addEventListener('click', startTimer);
pauseButton.addEventListener('click', pauseTimer);
resetButton.addEventListener('click', resetTimer);