debt-clock / static /embed.js
ntdservices's picture
Update static/embed.js
6172e7c verified
/* embed.js – discrete 4Hz ticking version */
(() => {
const API = "https://ntdservices-debt-clock.hf.space/api/debt";
const CLS = "ntd-debt-clock";
const REFRESH_MS = 5 * 60 * 1000; // poll backend every 5 min
/* 1 ▸ inject DSEG-7 font once per page */
if (!document.getElementById("dseg-face")) {
const style = document.createElement("style");
style.id = "dseg-face";
style.textContent = `
@font-face {
font-family: "DSEG7";
src: url("https://cdn.jsdelivr.net/npm/@fontsource/dseg7-classic/files/dseg7-classic-latin-700-normal.woff2")
format("woff2");
font-weight: 700;
font-style: normal;
font-display: swap;
}`;
document.head.appendChild(style);
}
/* 2 ▸ mount one clock per matching element */
function mount(el) {
Object.assign(el.style, {
font: "700 5.5rem 'DSEG7', monospace",
color: "#ffb400",
textShadow: "0 0 8px rgba(255,180,0,.9)",
background: "transparent",
padding: "10px 18px",
borderRadius: "6px",
whiteSpace: "pre",
letterSpacing: ".6em",
userSelect: "none",
position: "relative",
});
let baseDebt = 0;
let ratePerSec = 0;
let baseTime = Date.now();
async function pull() {
try {
const d = await fetch(API).then(r => r.json());
baseDebt = d.startingDebt;
ratePerSec = d.ratePerSecond;
baseTime = Date.now();
} catch (err) {
console.error("Debt fetch failed:", err);
}
}
pull();
setInterval(pull, REFRESH_MS);
/* update 4×/sec (every 250ms) */
setInterval(() => {
const now = Date.now();
const delta = (now - baseTime) / 1000;
const debt = baseDebt + ratePerSec * delta;
el.textContent =
Math.round(debt).toLocaleString("en-US").replace(/,/g, " ");
}, 250);
}
document.querySelectorAll("." + CLS).forEach(mount);
})();