-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
53 lines (49 loc) · 961 Bytes
/
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
function MAIN($) {
const tasks = [
{ loop: $.seekLoop, delayMs: 500 },
{ loop: $.resizeLoop, delayMs: 100 }
];
const minDelayMs = 100;
let loops = [];
let n = 1;
let max = Math.floor(Math.max(...tasks.map(it => it.delayMs)) / minDelayMs);
let href = window.location.href;
init();
main();
function init() {
loops = [];
for (let { loop, delayMs } of tasks) {
let { init, main } = loop;
loops.push({
init,
main,
initDone: false,
execAt: Math.floor(delayMs / minDelayMs)
});
}
}
function main() {
let newHref = window.location.href;
if (newHref !== href) {
init();
href = newHref;
}
for (let loop of loops) {
if (!loop.initDone) {
try {
loop.init();
} catch (e) {
$.print(e);
continue;
}
loop.initDone = true;
loop.main();
continue;
}
if (n % loop.execAt) continue;
loop.main();
}
n = n === max ? 1 : n + 1;
setTimeout(main, minDelayMs);
}
}