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
20function slow_load(speed=10, not_allowed=[], callback) {
21
22 function onFinish(node, initial_display){
23 node.style.display = initial_display;
24 counter++;
25 ratio = counter / total * 100;
26
27 randomAudio();
28
29 if (ratio >= 100 && callback)
30 callback()
31 }
32
33 const all = document.getElementsByTagName("*");
34 let counter = 0;
35 let total = 0;
36 let i = 0;
37 for (element in all) {
38
39 const node = all[element];
40 if((node.childElementCount == 0) && (not_allowed.indexOf(node.tagName) === -1) && node.style){
41 i += getRandomInt(250 / speed, 500 / speed);
42 total++;
43 const initial_display = node.style.display;
44 node.style.display = "none";
45 setTimeout(() => { onFinish(node, initial_display);}, i);
46 }
47 }
48}
49
50function randomAudio(){
51 const random_audio = getRandomInt(0, loadAudio.length - 1);
52 playAudio(loadAudio[random_audio]);
53}