all repos — retro-website @ 48047c1913980b3eea2b6c0e219cbaedd1f142d1

res/js/slow_load.js (view raw)

 1const loadAudio = [
 2    new Audio(url = window.location.origin + "/res/sfx/os/loadelement1.ogg"),
 3    new Audio(url = window.location.origin + "/res/sfx/os/loadelement2.ogg"),
 4    new Audio(url = window.location.origin + "/res/sfx/os/loadelement3.ogg"),
 5    new Audio(url = window.location.origin + "/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().catch(()=>{});
18}
19
20const all = document.getElementsByTagName("*");
21let total = 0;
22
23function hide_all() {
24    total = 0;
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    let counter = 0;
38    let i = 0;
39
40    function onNodeLoaded(node){
41        
42        node.style.display = node.getAttribute("data-display");
43        counter++;
44        const ratio = counter / total * 100;
45        
46        playAudio(loadAudio[getRandomInt(0, loadAudio.length - 1)]);
47        
48        if (ratio >= 100 && callback)
49            callback()
50    }
51    
52    for (element in all) {
53        const node = all[element];
54        if((node.childElementCount == 0) && (not_allowed.indexOf(node.tagName) === -1) && node.style){
55            i += getRandomInt(250 / speed, 500 / speed);
56            setTimeout(() => { onNodeLoaded(node);}, i);
57        }
58    }
59}