-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (119 loc) · 4.57 KB
/
Copy pathscript.js
File metadata and controls
133 lines (119 loc) · 4.57 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
let activeTimers = [];
const startTimerBtn = document.getElementById("start-btn");
const audio = new Audio("./audio/alarm.mp3");
startTimerBtn.addEventListener("click", (event) => {
event.preventDefault();
const hours = parseInt(document.getElementById("hours").value);
const minutes = parseInt(document.getElementById("minutes").value);
const seconds = parseInt(document.getElementById("seconds").value);
let totalSeconds = hours * 3600 + minutes * 60 + seconds;
// check for valid inputs
if (
hours < 0 ||
minutes < 0 ||
seconds < 0 ||
isNaN(hours) ||
isNaN(minutes) ||
isNaN(seconds)) {
alert("Please enter valid time");
return;
}
timeFormater(hours, minutes, seconds);
// modification needs after remove all active timer
document.getElementById("t-update").style.display = "none";
const timerId = `timer-${Date.now()}`; // unique id for each timer
const timerDisplay = document.createElement("div");
timerDisplay.setAttribute("class", "timer-box");
timerDisplay.id = timerId;
// p tag
const p = document.createElement("p");
p.innerText = "Time Left :";
timerDisplay.append(p);
const remainingTimeDiv = document.createElement("div");
remainingTimeDiv.setAttribute("class", "time-left");
remainingTimeDiv.innerHTML = `<span>${hours}h</span><span> :
</span><span>${minutes}m</span><span> :
</span><span>${seconds}s</span>`;
timerDisplay.append(remainingTimeDiv);
// input button
const input = document.createElement("input");
input.setAttribute("type", "button");
input.setAttribute("class", "btn");
input.setAttribute("value", "Delete");
input.id = `delete-btn`;
timerDisplay.append(input);
document.getElementById("active-timers").appendChild(timerDisplay);
const timeSchedule = {
id: timerId,
totalSeconds: totalSeconds,
intervalId: setInterval(() => {
updateTimerDisplay(timerId);
}, 1000),
};
activeTimers.push(timeSchedule);
// updateTimerDisplay(timerId);
});
function updateTimerDisplay(timerId) {
const currentTimer = document.getElementById(timerId);
const time = activeTimers.find((timer) => timer.id === timerId);
let remainingSeconds = time.totalSeconds - 1;
// console.log(timerId);
// time up
if (remainingSeconds <= 0) {
currentTimer.classList.add("time-up");
currentTimer.children[0].innerHTML = "";
currentTimer.children[1].innerText = "Time is Up !";
let stopTimerBtn = currentTimer.children[2];
stopTimerBtn.classList.add("time-up-btn");
audio.play();
// stop timer
// clearInterval(time.intervalId);
stopTimerBtn.setAttribute("value", "Stop");
stopTimerBtn.addEventListener("click", (event) => {
event.preventDefault();
audio.pause();
stopTimer(timerId);
});
} else {
time.totalSeconds = remainingSeconds;
const hours = Math.floor(remainingSeconds / 3600);
const minutes = Math.floor((remainingSeconds % 3600) / 60);
const seconds = remainingSeconds % 60;
currentTimer.children[1].innerHTML = `<span>${hours}h</span><span> :
</span><span>${minutes}m</span><span> :
</span><span>${seconds}s</span>`;
const deleteTimerBtn = currentTimer.children[2];
deleteTimerBtn.addEventListener("click", (event) => {
event.preventDefault();
clearInterval(time.intervalId);
removeTimerFromActiveTimers(timerId);
});
}
}
// stop timer
function stopTimer(timerId) {
const timer = activeTimers.find((time) => time.id === timerId);
if (timer) {
clearInterval(timer.intervalId);
removeTimerFromActiveTimers(timerId);
}
}
// delete timer
function removeTimerFromActiveTimers(timerId) {
activeTimers = activeTimers.filter((time) => time.id !== timerId);
const displayTimer = document.getElementById(timerId);
if (displayTimer) {
displayTimer.parentNode.removeChild(displayTimer);
}
// all active timers has removed from activeTimers[]
if (activeTimers.length === 0)
document.getElementById("t-update").style.display = "block";
}
function timeFormater(hh, mm, ss) {
if (hh < 10)
document.getElementById("hours").value = hh.toString().padStart(2, "0");
if (mm < 10)
document.getElementById("minutes").value = mm.toString().padStart(2, "0");
if (ss < 10)
document.getElementById("seconds").value = ss.toString().padStart(2, "0");
}