-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.js
151 lines (130 loc) · 4.61 KB
/
timer.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const timeText = document.querySelector(".time");
const timerForm = document.getElementById("timer-form");
const formSecs = document.getElementById("seconds");
const playBtn = document.querySelector(".play-button");
const playIcon = document.getElementById("play-icon");
const restartBtn = document.querySelector(".restart-button");
const audioBtn = document.querySelector(".lap-button");
const audioIcon = document.getElementById("audio-icon");
const timerIcon = document.querySelector(".timer i");
const timerNav = document.querySelector(".timer");
const timerUpModal = document.getElementById("timerUpModal");
const closeModalBtn = document.getElementById("closeModal");
const restartTimerBtn = document.getElementById("restartTimer");
let hours = document.querySelector(".mins");
let minsAndSecs = document.querySelector(".blue-text");
let alertHrs = document.querySelector(".alert-hrs");
let alertMinsSecs = document.querySelector(".alert-time");
// TIMER
let initialTime = (timeCounter = 0);
let stopwatchRunning = false;
let intervalId;
let audioMute = false;
const ringtone = new Audio("/ringtone.mp3");
let animatePhase = 0;
const timerAnimation = {
0: "fa-hourglass-start",
1: "fa-hourglass-half",
2: "fa-hourglass-end",
};
// FUNCTIONS
const animateIcon = function (reset = false) {
if (animatePhase > 1 || reset) animatePhase = 0;
else animatePhase++;
timerIcon.classList.remove(
timerAnimation[animatePhase == 0 ? 2 : animatePhase - 1]
);
timerIcon.classList.add(timerAnimation[animatePhase]);
};
const calcTime = function (unit, time = timeCounter) {
if (unit === "hours") return Math.floor(time / 60 / 60);
if (unit === "minutes") return Math.floor((time % 3600) / 60);
if (unit === "seconds") return Math.floor(time % 60);
};
const formatTime = (timeNum) => (timeNum + "").padStart(2, "0");
const updateDOMTime = function (
hrs = hours,
minsSecs = minsAndSecs,
time = timeCounter
) {
hrs.textContent = formatTime(calcTime("hours", time));
minsSecs.textContent = `${formatTime(calcTime("minutes", time))}:${formatTime(
calcTime("seconds", time)
)}`;
};
timerForm.addEventListener("submit", function (e) {
e.preventDefault();
initialTime = timeCounter =
+timerForm.elements.hours.value * 3600 +
+timerForm.elements.minutes.value * 60 +
+timerForm.elements.seconds.value;
updateDOMTime();
});
const startTimer = function () {
if (!stopwatchRunning) {
stopwatchRunning = true;
if (initialTime === timeCounter) timerForm.requestSubmit();
timerForm.classList.add("hidden");
timeText.classList.remove("hidden");
playIcon.classList.remove("fa-play");
playIcon.classList.add("fa-pause");
restartBtn.classList.remove("hidden");
audioBtn.classList.remove("hidden");
intervalId = setInterval(() => {
if (timeCounter > 0) timeCounter--;
updateDOMTime();
animateIcon();
if (timeCounter < 1) {
timerUpModal.style.display = "block";
updateDOMTime(alertHrs, alertMinsSecs, initialTime);
restartTimer(false);
timerIcon.classList.add("fa-shake");
if (!audioMute) ringtone.play();
}
}, 1000);
} else {
clearInterval(intervalId);
stopwatchRunning = false;
playIcon.classList.remove("fa-pause");
playIcon.classList.add("fa-play");
}
};
const restartTimer = function (clear = true, initTime = 0) {
if (clear) {
timeText.classList.add("hidden");
timerForm.classList.remove("hidden");
formSecs.focus();
timerForm.reset();
}
clearInterval(intervalId);
initialTime = timeCounter = initTime;
stopwatchRunning = false;
playIcon.classList.remove("fa-pause");
playIcon.classList.add("fa-play");
restartBtn.classList.add("hidden");
audioBtn.classList.add("hidden");
};
const closeWindow = function (restartTime = true) {
timerUpModal.style.display = "none";
timerIcon.classList.remove("fa-shake");
ringtone.pause();
ringtone.currentTime = 0;
restartTime ? restartTimer() : startTimer();
};
// EVENTS
playBtn.addEventListener("click", startTimer);
restartBtn.addEventListener("click", restartTimer);
closeModalBtn.addEventListener("click", closeWindow);
restartTimerBtn.addEventListener("click", () => {
closeWindow(false);
});
audioBtn.addEventListener("click", function () {
audioIcon.classList.toggle("fa-volume-high");
audioIcon.classList.toggle("fa-volume-xmark");
if (audioIcon.classList.contains("fa-volume-high")) audioMute = false;
else audioMute = true;
});
// Focuses the form's text cursor when reloading
formSecs.focus();
document.addEventListener("DOMContentLoaded", () => formSecs.focus());
timerNav.addEventListener("click", () => formSecs.focus());