blob: 84230a54166f87d8c28da0286fab05b23cc25f52 (
plain)
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
|
"use strict";
import { updateTime } from './block_time.js';
import { updateWeather } from './block_weather.js';
const config = {
polling: true,
pollingDelay: 500, // in ms. To be light on CPU make it >= 500ms
}
function init() {
const blocks = {
time: { interval: 30 * 1000, lastUpdate: 0, update: updateTime }, // 30s
weather: { interval: 30 * 60000, lastUpdate: 0, update: updateWeather } // 30min
// Add more: { interval: X, lastUpdate: 0, update: updateFunction }
};
initHeaderControls(blocks)
reloadAll(blocks);
setInterval(() => pollUpdates(blocks), config.pollingDelay);
}
function reloadAll(blocks) {
Object.keys(blocks).forEach(key => {
blocks[key].update();
blocks[key].lastUpdate = Date.now();
});
}
function pollUpdates(blocks) {
if (!config.polling) return;
const now = Date.now();
Object.keys(blocks).forEach(key => {
const block = blocks[key];
if (now - block.lastUpdate >= block.interval) {
block.update();
block.lastUpdate = now;
}
});
}
function initHeaderControls(blocks) {
document.getElementById("reload").addEventListener("click", () => reloadAll(blocks));
document.getElementById("pause").addEventListener("change", (e) => {
config.polling = e.target.checked;
});
config.polling = document.getElementById("pause").checked;
document.getElementById("city").addEventListener("keypress", (e) => {
if (e.key === "Enter") {
updateWeather()
}
});
}
init()
|