-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (91 loc) · 2.95 KB
/
Copy pathscript.js
File metadata and controls
114 lines (91 loc) · 2.95 KB
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
let pomodoro = document.getElementById("pomodoro-timer")
let short = document.getElementById("short-timer")
let long = document.getElementById("long-timer")
let timers = document.querySelectorAll(".timer-display")
let session = document.getElementById("pomodoro-session")
let shortBreak = document.getElementById("short-break")
let longBreak = document.getElementById("long-break")
let startBtn = document.getElementById("start")
let stopBtn = document.getElementById("stop")
let timerMsg = document.getElementById("timer-message")
let button = document.querySelector(".button")
let currentTimer = null
let myInterval = null
// show the default timer
function showDefaultTimer() {
pomodoro.style.display = "block"
short.style.display = "none"
long.style.display = "none"
}
showDefaultTimer()
function hideAll() {
timers.forEach((timer) => (
timer.style.display = "none"
))
}
session.addEventListener("click", () => {
hideAll()
pomodoro.style.display = "block"
session.classList.add("active")
shortBreak.classList.remove("active")
longBreak.classList.remove("active")
currentTimer = pomodoro
})
shortBreak.addEventListener("click", () => {
hideAll()
short.style.display = "block"
session.classList.remove("active")
shortBreak.classList.add("active")
longBreak.classList.remove("active")
currentTimer = short
})
longBreak.addEventListener("click", () => {
hideAll()
long.style.display = "block"
session.classList.remove("active")
shortBreak.classList.remove("active")
longBreak.classList.add("active")
currentTimer = long
})
// Start the timer on click
function startTimer(timerDisplay) {
if (myInterval) {
clearInterval(myInterval);
}
timerDuration = timerDisplay
.getAttribute("data-duration")
.split(":")[0];
let durationinmiliseconds = timerDuration * 60 * 1000;
let endTimestamp = Date.now() + durationinmiliseconds;
myInterval = setInterval(function () {
const timeRemaining = new Date(endTimestamp - Date.now());
if (timeRemaining <= 0) {
clearInterval(myInterval);
timerDisplay.textContent = "00:00";
const alarm = new Audio(
"https://www.freespecialeffects.co.uk/soundfx/scifi/electronic.wav"
);
alarm.play();
} else {
const minutes = Math.floor(timeRemaining / 60000);
const seconds = ((timeRemaining % 60000) / 1000).toFixed(0);
const formattedTime = `${minutes}:${seconds
.toString()
.padStart(2, "0")}`;
timerDisplay.textContent = formattedTime;
}
}, 1000);
}
startBtn.addEventListener("click", () => {
if (currentTimer) {
startTimer(currentTimer)
timerMsg.style.display = "none"
} else {
timerMsg.style.display = "block"
}
})
stopBtn.addEventListener("click", () => {
if(currentTimer) {
clearInterval(myInterval)
}
})