all repos — retro-website @ c28f8a2fe33f5a349ae848bebf6f5da189332e28

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    console.log("hide");
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    console.log("show");
37
38    function onNodeLoaded(node){
39        node.style.display = node.getAttribute("data-display");
40        counter++;
41        ratio = counter / total * 100;
42        
43        playAudio(loadAudio[getRandomInt(0, loadAudio.length - 1)]);
44    
45        if (ratio >= 100 && callback)
46            callback()
47    }
48    
49    let counter = 0;
50    let i = 0;
51    for (element in all) {
52        const node = all[element];
53        if((node.childElementCount == 0) && (not_allowed.indexOf(node.tagName) === -1) && node.style){
54
55            i += getRandomInt(250 / speed, 500 / speed);
56            setTimeout(() => { onNodeLoaded(node);}, i);
57        }
58    }
59}