diff options
| author | littleblack111 <littleblack11111@gmail.com> | 2023-11-04 13:19:32 +0800 |
|---|---|---|
| committer | littleblack111 <littleblack11111@gmail.com> | 2023-11-04 13:19:32 +0800 |
| commit | 77ae8b37e7d8b35835b26d7ff8c97614f2ffbb7e (patch) | |
| tree | 935d8afc2e0379b87a78c40fb85f2154629e62a4 /src/js | |
| parent | 421a210bf0cad79e8a31855bb8a9ccb6844550c7 (diff) | |
fix for multi run
Diffstat (limited to 'src/js')
| -rw-r--r-- | src/js/hacktype.js | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/src/js/hacktype.js b/src/js/hacktype.js index eb5595c0..4ff0463a 100644 --- a/src/js/hacktype.js +++ b/src/js/hacktype.js @@ -6,45 +6,45 @@ let interval = null; function hackType() { const elements = document.querySelectorAll(".hacktype"); -let intervals = []; +const intervals = []; elements.forEach((element) => { let iteration = 0; - intervals.push( - setInterval(() => { - element.innerText = element.innerText + const interval = setInterval(() => { + element.innerText = element.innerText .split("") .map((letter, index) => { - if (index < iteration) { + if (index < iteration) { return element.dataset.value[index]; - } + } - return letters[Math.floor(Math.random() * 26)]; + return letters[Math.floor(Math.random() * 26)]; }) .join(""); - if (iteration >= element.dataset.value.length) { - clearInterval(intervals.shift()); - } + if (iteration >= element.dataset.value.length) { + clearInterval(interval); + } + + iteration += 1 / 5; + }, 20); - iteration += 1 / 5; - }, 20) - ); + intervals.push(interval); }); } hackType(); // Call hackType initially setInterval(() => { -const intervals = document.querySelectorAll(".hacktype-interval"); -intervals.forEach((interval) => clearInterval(interval)); // Clear existing intervals +const elements = document.querySelectorAll(".hacktype"); +elements.forEach((element) => clearInterval(interval)); // Clear existing intervals hackType(); // Call hackType again -}, 10000); // Repeat every 5 seconds +}, 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 } -}); +});
\ No newline at end of file |
