-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
151 lines (135 loc) · 4.55 KB
/
Copy pathapp.js
File metadata and controls
151 lines (135 loc) · 4.55 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// DOM Imports
const addAlarm = document.getElementById('add-alarm')
const stopAlarm = document.getElementById('stop')
const time = document.getElementById('time')
const hours = document.getElementById('hours')
const minutes = document.getElementById('minutes')
const seconds = document.getElementById('seconds')
const AMPM = document.getElementById('AMPM')
const alarmInput = document.getElementById('alarm-input')
const inputTime = document.getElementById('input-time')
const closeBtn = document.getElementById('close')
const setTime = document.getElementById('set-time')
const resetTime = document.getElementById('reset-time')
const upcomingAlarms = document.getElementById('upcoming-alarms')
// Functional Constants
const alarmSound = new Audio('./sounds/alarm.mp3')
alarmSound.loop = true
// Event Listeners
addAlarm.addEventListener('click', () => {
alarmInput.classList.add('active')
})
closeBtn.addEventListener('click', () => {
alarmInput.classList.remove('active')
})
setTime.addEventListener('click', () => {
setAlarmTime(inputTime.value)
})
resetTime.addEventListener('click', () => (inputTime.value = ''))
// Utility Functions
function setAlarmTime(time) {
let alarmTime = time
alarmTime = alarmTime + ':00'
alarmTime = alarmTime.split(':')
setAlarm(alarmTime)
}
function setAlarm(time) {
let alarmActive = true
let AP = ' am'
if (+time[0] > 12) {
time[0] = +time[0] - 12
time = time.join(':')
AP = ' pm'
time += AP
} else {
time = time.join(':')
time += AP
}
// main alarm
const nextAlarm = document.createElement('div')
nextAlarm.classList.add('next-alarm')
nextAlarm.innerHTML = `
<input type="text" value="${time}" id="edit-alarm">
<button id='delete-alarm'> <i class='fa-solid fa-trash'></i> </button>
<span>Click the time to edit alarm and trash to delete alarm</span>
`
upcomingAlarms.firstElementChild.classList.add('active')
upcomingAlarms.appendChild(nextAlarm)
addAlarm.disabled = true
addAlarm.style.cursor = 'not-allowed'
// edit alarm
nextAlarm.firstElementChild.addEventListener('click', () => {
const confirmDiv = document.createElement('div')
confirmDiv.classList.add('confirm-div')
confirmDiv.innerHTML = `
<input type="time" id="input-time" />
<i class="fa-solid fa-circle-check"></i>
`
nextAlarm.firstElementChild.value = ''
nextAlarm.firstElementChild.nextElementSibling.remove()
confirmDiv.lastElementChild.addEventListener('click', () => {
if (confirmDiv.firstElementChild.value) {
setAlarmTime(confirmDiv.firstElementChild.value)
console.log(confirmDiv.firstElementChild.value)
nextAlarm.remove()
}
})
nextAlarm.appendChild(confirmDiv)
})
// delete alarm
nextAlarm.firstElementChild.nextElementSibling.addEventListener(
'click',
() => {
nextAlarm.remove()
addAlarm.disabled = false
upcomingAlarms.firstElementChild.classList.remove('active')
addAlarm.style.cursor = 'pointer'
}
)
// Function to check alarm time to current time
const clearCheck = setInterval(() => {
const timeString = new Date()
let currentTime = timeString.toLocaleTimeString().toLocaleLowerCase()
if (currentTime == time) {
alarmSound.play()
alarmActive = false
stopAlarm.style.display = 'block'
nextAlarm.remove()
upcomingAlarms.firstElementChild.classList.remove('active')
}
}, 1000)
// Stop Button
stopAlarm.addEventListener('click', () => {
if (!alarmActive) {
alarmSound.pause()
clearInterval(clearCheck)
addAlarm.disabled = false
upcomingAlarms.firstElementChild.classList.remove('active')
addAlarm.style.cursor = 'pointer'
stopAlarm.style.display = 'none'
nextAlarm.remove()
}
})
inputTime.value = ''
alarmInput.classList.remove('active')
}
// Function to display current time updating every second
function updateTime() {
const dateObj = new Date()
const currentHours = dateObj.getHours()
const currentMinutes = dateObj.getMinutes()
const currenSeconds = dateObj.getSeconds()
const formatHours = currentHours % 12
const currentAMPM = hours < 12 ? 'AM' : 'PM'
hours.textContent = addZero(formatHours)
minutes.textContent = addZero(currentMinutes)
seconds.textContent = addZero(currenSeconds)
AMPM.textContent = currentAMPM
}
function addZero(time) {
if (time < 10) {
return '0' + time
}
return time
}
setInterval(updateTime, 1000)