summaryrefslogtreecommitdiff
path: root/src/js/hacktype.js
blob: 44553b29dc628bfaf1e5aeb0fd7f329bc863e3e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const numbers = "1234567890";
const special = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";

let interval = null;

function hackType() {
    const elements = document.querySelectorAll(".hacktype");
    const intervals = [];

    elements.forEach((element) => {
        let iteration = 0;

        const interval = setInterval(() => {
        element.innerText = element.innerText
            .split("")
            .map((letter, index) => {
            if (index < iteration) {
                return element.dataset.value[index];
            }

            return letters[Math.floor(Math.random() * 26)];
            })
            .join("");

        if (iteration >= element.dataset.value.length) {
            clearInterval(interval);
        }

        iteration += 1 / 5;
        }, 20);

        intervals.push(interval);
    });
}

hackType(); // Call hackType initially

setInterval(() => {
const elements = document.querySelectorAll(".hacktype");
    elements.forEach((element) => clearInterval(interval)); // Clear existing intervals
    hackType(); // Call hackType again
}, 10000); // Repeat every 10 seconds

// Check if the document becomes visible again
document.addEventListener("visibilitychange", () => {
    if (!document.hidden) {
        hackType(); // Call hackType when the user comes back from being away
    }
});