mirror of
https://github.com/zyllian/webdog.git
synced 2025-01-18 19:52:22 -08:00
30 lines
814 B
JavaScript
30 lines
814 B
JavaScript
|
(function () {
|
||
|
"use strict";
|
||
|
|
||
|
const DEBOUNCE_TIMER = 1500;
|
||
|
|
||
|
const url = document.body.classList.contains("debug")
|
||
|
? "http://127.0.0.1:8787/api/pet"
|
||
|
: "https://cf.zyllian.workers.dev/api/pet";
|
||
|
|
||
|
const petCounter = document.querySelector("#pet-counter");
|
||
|
const count = petCounter.querySelector(".count");
|
||
|
const petButton = petCounter.querySelector("button");
|
||
|
|
||
|
(async function () {
|
||
|
const r = await (await fetch(url)).json();
|
||
|
count.innerText = r.count;
|
||
|
})();
|
||
|
|
||
|
petButton.addEventListener("click", async () => {
|
||
|
petButton.disabled = true;
|
||
|
setTimeout(() => (petButton.disabled = false), DEBOUNCE_TIMER);
|
||
|
const r = await (await fetch(url, { method: "post" })).json();
|
||
|
if (r.count) {
|
||
|
count.innerText = r.count;
|
||
|
}
|
||
|
});
|
||
|
|
||
|
petCounter.style.display = "block";
|
||
|
})();
|