-
Notifications
You must be signed in to change notification settings - Fork 900
/
Copy pathcountdown.js
67 lines (58 loc) · 2.05 KB
/
countdown.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
let minutesTarget = 5; // Change the target minutes as needed
let hoursTarget = 2; // Change the target hours as needed
let secondsTarget = 30; // Change the target seconds as needed
function updateCards() {
updateCard('minutes', minutesTarget);
updateCard('hours', hoursTarget);
updateCard('seconds', secondsTarget);
}
function updateCard(card, value) {
const front = document.getElementById(`${card}Front`);
const back = document.getElementById(`${card}Back`);
back.textContent = value < 10 ? `0${value}` : value;
const cardElement = document.getElementById(`${card}Card`);
cardElement.classList.add('flipped');
setTimeout(() => {
front.textContent = back.textContent;
cardElement.classList.remove('flipped');
}, 500);
}
function updateCountdown() {
if (secondsTarget > 0) {
secondsTarget--;
updateCard('seconds', secondsTarget);
} else {
if (minutesTarget > 0) {
minutesTarget--;
secondsTarget = 59;
updateCard('minutes', minutesTarget);
updateCard('seconds', secondsTarget);
} else {
if (hoursTarget > 0) {
hoursTarget--;
minutesTarget = 59;
secondsTarget = 59;
updateCard('hours', hoursTarget);
updateCard('minutes', minutesTarget);
updateCard('seconds', secondsTarget);
}
}
}
}
let countdownInterval;
document.getElementById('startButton').addEventListener('click', () => {
clearInterval(countdownInterval);
countdownInterval = setInterval(updateCountdown, 1000);
});
document.getElementById('pauseButton').addEventListener('click', () => {
clearInterval(countdownInterval);
});
document.getElementById('resetButton').addEventListener('click', () => {
clearInterval(countdownInterval);
minutesTarget = 5; // Reset to the initial target minutes
hoursTarget = 2; // Reset to the initial target hours
secondsTarget = 30; // Reset to the initial target seconds
updateCards();
});
// Initial update
updateCards();