res/slow_load.js (view raw)
1
2
3function slow_load(speed=0.5, not_allowed=[], callback) {
4
5 function onFinish(node, initial_display){
6 node.style.display = initial_display;
7 counter++;
8 ratio = counter / total * 100;
9
10 if (ratio >= 100 && callback)
11 callback()
12 }
13
14 function getRandomInt(min, max) {
15 min = Math.ceil(min);
16 return Math.floor(Math.random() * (Math.floor(max) - min + 1)) + min;
17 }
18
19 const all = document.getElementsByTagName("*");
20 let counter = 0;
21 let total = 0;
22 let i = 0;
23 for (i in all) {
24 const node = all[i];
25 if((node.childElementCount == 0) && (not_allowed.indexOf(node.tagName) === -1) && node.style){
26 i += getRandomInt(5 / speed, 20 / speed);
27 total++;
28 const initial_display = node.style.display;
29 node.style.display = "none";
30 setTimeout(() => { onFinish(node, initial_display);}, i);
31 }
32 }
33}
34
35const not_allowed = ["META", "TITLE", "LINK", "SOURCE", "SCRIPT"];
36
37function finishedCallback() {
38 console.log("FINISHED!");
39}
40
41// page logic
42slow_load(speed=1.25, not_allowed, finishedCallback);