Repository navigation
Expand file tree
/
Copy pathticktock.js
More file actions
84 lines (70 loc) · 2.06 KB
/
Copy pathticktock.js
File metadata and controls
84 lines (70 loc) · 2.06 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
(() => {
let displayHex = false;
let previousSecond = 0;
const $ = (id) => document.getElementById(id);
const normalise = (scalar, maxValue, divisions) =>
Math.floor(parseFloat(scalar, 10) * maxValue / divisions);
const stringify = (number, hexOutput = false) => {
const num = ~~number;
if (hexOutput) {
return num.toString(16).padStart(2, '0');
}
return num.toString().padStart(2, '0');
};
const getBackgroundColor = (h, m, s) => {
const normH = normalise(h, 255, 24);
const normM = normalise(m, 255, 60);
const normS = normalise(s, 255, 60);
return `rgb(${normH},${normM},${normS})`;
};
const flash = () => {
const colons = document.querySelectorAll('.colon');
colons.forEach(colon => {
colon.style.visibility = colon.style.visibility === 'hidden' ? 'visible' : 'hidden';
});
};
const tick = () => {
const time = new Date();
const s = time.getSeconds();
const m = time.getMinutes();
const h = time.getHours();
const hours = $('hours');
const minutes = $('minutes');
const seconds = $('seconds');
if (displayHex) {
hours.textContent = stringify(normalise(h, 255, 24), true);
minutes.textContent = stringify(normalise(m, 255, 60), true);
seconds.textContent = stringify(normalise(s, 255, 60), true);
} else {
hours.textContent = stringify(h);
minutes.textContent = stringify(m);
seconds.textContent = stringify(s);
}
if (s !== previousSecond) {
flash();
previousSecond = s;
}
document.body.style.backgroundColor = getBackgroundColor(h, m, s);
};
const init = () => {
// Remove "need JavaScript" message
const message = $('message');
if (message) {
message.remove();
}
// Toggle between hex and normal time display on click
document.addEventListener('click', () => {
displayHex = !displayHex;
tick(); // Update immediately
}, { passive: true });
// Initial tick and set up interval
tick();
setInterval(tick, 1000);
};
// Start when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();