Repository navigation
Expand file tree
/
Copy pathclock.js
More file actions
169 lines (127 loc) · 4.63 KB
/
Copy pathclock.js
File metadata and controls
169 lines (127 loc) · 4.63 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Author: Ivan Rocha
Date: 08/18/2017
Notes:
- Color palette adapted from color.adobe.com project "The Clock Strikes Nine" by user nicolagilroy ( https://color.adobe.com/The-Clock-Strikes-Nine-color-theme-1294130/ )
- "Alarm Clock" font from dafont.com, created by David J. Patterson ( http://www.dafont.com/alarm-clock.font )
*/
document.addEventListener("DOMContentLoaded", function() {
// Selectors
var wrapper = document.getElementById('wrapper');
var alarmHours = document.getElementById('alarm_hours');
var alarmMinutes = document.getElementById('alarm_minutes');
var alarmName = document.getElementById('alarm_name');
var alarmAMPM = document.getElementById('alarm_ampm');
var submit = document.getElementById('submit');
var alarmMessage = document.getElementById('alarm_message');
var alarmList = document.getElementById('alarm_list');
// Create array to store alarm times in
var alarms = [];
// Create alarm elements
createAlarmElements();
// Render initial time, then update every second after
renderTime();
setInterval(renderTime, 1000);
// Set event listener for adding alarms
addListenerForInput();
// Alarm constructor
function Alarm(timeArg, nameArg){
this.time = timeArg;
this.name = nameArg;
this.getTime = function(){
return this.time;
}
this.getName = function(){
return this.name;
}
}
// Functions
function createAlarmElements(){
for (i=1; i <= 12; i++){
var hourOption = document.createElement('option');
hourOption.innerHTML = '<option value="' + i + '">' + i + '</option>';
alarmHours.appendChild(hourOption);
}
for (i=0; i < 60; i++){
// Pad minutes
var output;
if (i < 10){
output = '0' + i;
}
else{
output = i;
}
var minuteOption = document.createElement('option');
minuteOption.innerHTML = '<option value="' + output + '">' + output + '</option>';
alarmMinutes.appendChild(minuteOption);
}
}
// Render time
function renderTime(){
// create new date object
var time = new Date();
var ampm = 'AM';
// Get values
var timeSeconds = time.getSeconds();
var timeMinutes = time.getMinutes();
var timeHours = time.getHours();
// Format values
if (timeHours > 12){
timeHours -= 12;
ampm = 'PM'
}
if (timeMinutes < 10){
timeMinutes = '0' + timeMinutes;
}
if (timeSeconds < 10){
timeSeconds = '0' + timeSeconds;
}
// Display time
wrapper.innerHTML = timeHours + ':' + timeMinutes + ':' + timeSeconds + ' ' + ampm;
// Check if time matches alarm time
if (alarms.length > 0){
for (var i = 0; i < alarms.length; i++){
var checkAlarm = alarms[i].getTime();
if (checkAlarm.getHours() == timeHours && checkAlarm.getMinutes() == timeMinutes){
ringAlarm(alarms[i].getName());
}
}
}
}
// Add a listener to the alarm input
function addListenerForInput(){
submit.addEventListener('click', setAlarm);
}
// Set alarm
function setAlarm(){
if (alarmName.value && alarmHours.value && alarmMinutes.value && alarmAMPM.value){
var time = new Date();
if (alarmAMPM.value == "PM"){
time.setHours(alarmHours.value + 12);
}
else {
time.setHours(alarmHours.value);
}
time.setMinutes(alarmMinutes.value);
time.setSeconds(0);
// Create new alarm object and store it in alarms[]
var newAlarm = new Alarm(time, alarmName.value);
alarms.push(newAlarm);
// Update alarm message text
alarmMessage.innerHTML = "Your alarms";
// Append date to list of dates in DOM
var newListItem = document.createElement('li');
newListItem.innerHTML = alarmName.value + " - " + time.toLocaleTimeString();
alarmList.appendChild(newListItem);
//TODO: Display the alarms in chronological order
}
else{
alert('ERROR: valid time and name needed to set an alarm');
}
}
// Do something when the alarm goes off
function ringAlarm(alarmInput){
// TODO: Add animations and possibly sound
alert(alarmInput);
}
});