-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
134 lines (103 loc) · 2.8 KB
/
main.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
const onClass = "on";
const offClass = "off";
const second_lights = [
["", "s12", "s11", "s10"],
["s03", "s02", "s01", "s00"]
];
const minute_lights = [
["", "m12", "m11", "m10"],
["m03", "m02", "m01", "m00"]
];
const hour_lights = [
["", "", "h11", "h10"],
["h03", "h02", "h01", "h00"]
];
function doDate()
{
var str = "";
const now = new Date();
let date = now.getDate();
let month = now.getMonth();
let year = now.getFullYear();
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
// Get parts
let hourHi = Math.floor(hours / 10);
let hourLo = hours % 10;
let minHi = Math.floor(minutes / 10);
let minLo = minutes % 10;
let secHi = Math.floor(seconds / 10);
let secLo = seconds % 10;
let secHiBits = toBinary(secHi);
let secLoBits = toBinary(secLo);
let minHiBits = toBinary(minHi);
let minLoBits = toBinary(minLo);
let hrHiBits = toBinary(hourHi);
let hrLoBits = toBinary(hourLo);
var hourString = "" + hours;
if (hourString.length === 1) {
hourString = "0" + hourString;
}
var minString = "" + minutes;
if (minString.length === 1) {
minString = "0" + minString;
}
var secString = "" + seconds;
if (secString.length === 1) {
secString = "0" + secString;
}
str += hourString + ":" + minString + ":" + secString;
let log = document.getElementById("log");
log.innerText = str;
toggleClockBits(secHiBits, secLoBits, second_lights);
toggleClockBits(minHiBits, minLoBits, minute_lights);
toggleClockBits(hrHiBits, hrLoBits, hour_lights);
}
function toggleClockBits(hiBits, lowBits, elementIDs) {
toggleBits(lowBits, elementIDs[1]);
toggleBits(hiBits, elementIDs[0]);
}
function toggleBits(bits, elementIDs) {
for(let index = 0; index < bits.length; index++) {
let id = elementIDs[index];
if(id === "") {
continue;
}
let light = document.getElementById(id);
if (bits[index] === "1") {
turnOn(light);
} else {
turnOff(light)
}
}
}
function turnOn(element) {
element.classList.remove(offClass);
element.classList.add(onClass);
}
function turnOff(element) {
element.classList.remove(onClass);
element.classList.add(offClass);
}
function toBinary(base10Num) {
var out = "";
var raised = 1;
while(base10Num >= raised) {
if ((raised & base10Num) > 0) {
out = "1" + out;
} else {
out = "0" + out;
}
raised *= 2;
}
var pad = 4 - out.length
while(pad > 0) {
out = "0" + out;
pad--;
}
return out;
}
document.addEventListener("DOMContentLoaded", function() {
setInterval(doDate, 850);
});