-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (52 loc) · 1.86 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
const bells = new Audio('./sounds/bell.wav');
const startBtn = document.querySelector('.btn-start');
const pauseBtn = document.querySelector('.btn-pause');
const resetBtn = document.querySelector('.btn-restart');
const session = document.querySelector('.minutes');
let myInterval;
let state = true;
const appTimer = () => {
const sessionAmount = Number.parseInt(session.textContent);
if(state) {
state = false;
if (!totalSeconds) {
totalSeconds = sessionAmount * 60; // Initialize totalSeconds if not already set
}
const updateSeconds = () => {
const minuteDiv = document.querySelector('.minutes');
const secondDiv = document.querySelector('.seconds');
totalSeconds--;
let minutesLeft = Math.floor(totalSeconds/60);
let secondsLeft = totalSeconds % 60;
if(secondsLeft < 10) {
secondDiv.textContent = '0' + secondsLeft;
} else {
secondDiv.textContent = secondsLeft;
}
minuteDiv.textContent = `${minutesLeft}`
if(minutesLeft === 0 && secondsLeft === 0) {
bells.play()
clearInterval(myInterval);
state = true; // Reset state when timer ends
totalSeconds = null; // Reset totalSeconds when timer ends
}
};
myInterval = setInterval(updateSeconds, 1000);
} else {
alert('Session has already started.')
}
};
startBtn.addEventListener('click', appTimer);
pauseBtn.addEventListener('click', () => {
clearInterval(myInterval);
state = true;
});
resetBtn.addEventListener('click', () => {
clearInterval(myInterval);
state = true;
totalSeconds = null; // Reset totalSeconds
const minuteDiv = document.querySelector('.minutes');
const secondDiv = document.querySelector('.seconds');
minuteDiv.textContent = '25';
secondDiv.textContent = '00';
});