forked from alirahimi818/simple-PWA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clock.js
51 lines (45 loc) · 1.29 KB
/
clock.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
// timer
window.addEventListener('load', function () {
const elemObject = {
hour: document.getElementById('hour'),
minute: document.getElementById('minute'),
second: document.getElementById('second'),
};
const lastValueObject = {
hour: null, minute: null, second: null
}
function setValue(elemId, value) {
const lastValue = lastValueObject[elemId];
if (value !== lastValue) {
const elem = elemObject[elemId];
lastValueObject[elemId] = value;
elem.innerText = value.toString().padStart(2, '0');
}
}
function tickClock() {
const d = new Date();
// hour
let h = d.getHours() + 1;
h = h > 12 ? h - 12 : h;
setValue('hour', h);
setValue('minute', d.getMinutes());
setValue('second', d.getSeconds());
}
this.setInterval(tickClock, 500);
});
// Always-on screen
if ('wakeLock' in navigator) {
window.addEventListener('load', async function () {
let wakeLock = null;
try {
wakeLock = await navigator.wakeLock.request('screen');
} catch (err) {
console.err('Caught error:', err);
}
document.addEventListener("visibilitychange", async () => {
if (wakeLock !== null && document.visibilityState === "visible") {
wakeLock = await navigator.wakeLock.request("screen");
}
});
});
}