res/slow_load.js (view raw)
1const loadAudio = [
2 new Audio(url="../res/sfx/os/loadelement1.ogg"),
3 new Audio(url="../res/sfx/os/loadelement2.ogg"),
4 new Audio(url="../res/sfx/os/loadelement3.ogg"),
5 new Audio(url="../res/sfx/os/loadelement4.ogg")
6]
7const not_allowed = ["AUDIO", "META", "TITLE", "LINK", "SOURCE", "SCRIPT", "BR"];
8
9function getRandomInt(min, max) {
10 min = Math.ceil(min);
11 return Math.floor(Math.random() * (Math.floor(max) - min + 1)) + min;
12}
13
14function playAudio(audio){
15 audio.volume = 0.1;
16 audio.currentTime = 0;
17 audio.play();
18}
19
20const all = document.getElementsByTagName("*");
21let total = 0;
22
23function hide_all() {
24
25 for (i in all) {
26 const node = all[i];
27 if((node.childElementCount == 0) && (not_allowed.indexOf(node.tagName) === -1) && node.style){
28 total++;
29 node.setAttribute("data-display", node.style.display);
30 node.style.display = "none";
31 }
32 }
33}
34
35function start_loading(speed, not_allowed=[], callback) {
36
37 function onNodeLoaded(node){
38 node.style.display = node.getAttribute("data-display");
39 counter++;
40 ratio = counter / total * 100;
41
42 playAudio(loadAudio[getRandomInt(0, loadAudio.length - 1)]);
43
44 if (ratio >= 100 && callback)
45 callback()
46 }
47
48 let counter = 0;
49 let i = 0;
50 for (element in all) {
51 const node = all[element];
52 if((node.childElementCount == 0) && (not_allowed.indexOf(node.tagName) === -1) && node.style){
53
54 i += getRandomInt(250 / speed, 500 / speed);
55 setTimeout(() => { onNodeLoaded(node);}, i);
56 }
57 }
58}