mirror of
https://github.com/YouHaveTrouble/youhavetrouble.github.io.git
synced 2026-05-12 06:16:55 +00:00
make visitor counter update every 10 seconds
This commit is contained in:
@@ -49,14 +49,27 @@
|
|||||||
<script>
|
<script>
|
||||||
|
|
||||||
updateViewCount();
|
updateViewCount();
|
||||||
|
let previousCount = 0;
|
||||||
|
|
||||||
async function updateViewCount() {
|
async function updateViewCount() {
|
||||||
const viewCounter = document.querySelector("[data-viewcount]");
|
const viewCounter = document.querySelector("[data-viewcount]");
|
||||||
if (!viewCounter) return;
|
if (!viewCounter) {
|
||||||
|
setTimeout(updateViewCount, 10000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
console.debug("Updating view count...");
|
||||||
const result = await fetch("https://youhavetrouble-view_counter.web.val.run").catch(() => null);
|
const result = await fetch("https://youhavetrouble-view_counter.web.val.run").catch(() => null);
|
||||||
if (result === null) return;
|
if (result === null) {
|
||||||
|
setTimeout(updateViewCount, 10000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
const json = await result.json();
|
const json = await result.json();
|
||||||
const rawCount = json.count;
|
const rawCount = json.count;
|
||||||
|
if (rawCount === previousCount) {
|
||||||
|
setTimeout(updateViewCount, 10000);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
previousCount = rawCount;
|
||||||
let chars = rawCount.toString();
|
let chars = rawCount.toString();
|
||||||
while (chars.length < 6) {
|
while (chars.length < 6) {
|
||||||
chars = "0" + chars;
|
chars = "0" + chars;
|
||||||
@@ -67,6 +80,7 @@
|
|||||||
span.textContent = chars[i];
|
span.textContent = chars[i];
|
||||||
viewCounter.appendChild(span);
|
viewCounter.appendChild(span);
|
||||||
}
|
}
|
||||||
|
setTimeout(updateViewCount, 10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user