mirror of
https://github.com/zyllian/webdog.git
synced 2025-05-09 18:16:40 -07:00
add global pet counter to site :3
This commit is contained in:
parent
0e444ec503
commit
29dcdc85cb
14 changed files with 3677 additions and 0 deletions
30
site/root/js/pet-me.js
Normal file
30
site/root/js/pet-me.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
(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";
|
||||
console.log(url);
|
||||
|
||||
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";
|
||||
})();
|
Loading…
Add table
Add a link
Reference in a new issue