ntdservices commited on
Commit
c318a2c
Β·
verified Β·
1 Parent(s): d2180f1

Update static/embed.js

Browse files
Files changed (1) hide show
  1. static/embed.js +43 -37
static/embed.js CHANGED
@@ -1,68 +1,74 @@
1
- /* embed.js – guaranteed 7-segment look */
2
  (() => {
3
- const API = "https://ntdservices-debt-clock.hf.space/api/debt";
4
- const CLS = "ntd-debt-clock";
 
5
 
6
- /* ── 1. inject a real DSEG7-Classic-Bold font once ─────────────── */
7
  if (!document.getElementById("dseg-face")) {
8
  const style = document.createElement("style");
9
- style.id = "dseg-face";
10
  style.textContent = `
11
  @font-face {
12
  font-family: "DSEG7";
13
- src:
14
- url("https://cdn.jsdelivr.net/npm/@fontsource/dseg7-classic/files/dseg7-classic-latin-700-normal.woff2") format("woff2");
15
  font-weight: 700;
16
  font-style: normal;
17
  font-display: swap;
18
- }
19
- `;
20
  document.head.appendChild(style);
21
  }
22
 
23
- /* ── 2. mount logic ─────────────────────────────────────────────── */
24
  function mount(el) {
25
  Object.assign(el.style, {
26
  font: "700 4rem 'DSEG7', monospace",
27
  color: "#ffb400",
28
- textShadow: "0 0 8px rgba(255, 180, 0, .9)",
29
  background: "transparent",
30
  padding: "10px 18px",
31
  borderRadius: "6px",
32
  whiteSpace: "nowrap",
33
  letterSpacing: ".06em",
34
- position: "absolute",
35
- top: "40%",
36
- left: "50%",
37
- transform: "translate(-50%, -50%)",
38
  userSelect: "none",
 
39
  });
40
 
41
- /* fetch current debt + per-second rate */
42
- fetch(API)
43
- .then(r => r.json())
44
- .then(d => {
45
- let debt = d.startingDebt;
46
- const rate = d.ratePerSecond;
47
- let last = performance.now();
48
-
49
- /* update 4Γ—/sec so it β€œticks” but doesn’t blur */
50
- setInterval(() => {
51
- const now = performance.now();
52
- const dt = (now - last) / 1000;
53
- last = now;
54
- debt += rate * dt;
55
 
56
- el.textContent =
57
- Math.round(debt).toLocaleString("en-US").replace(/,/g, " ");
58
-
59
- }, 250);
60
- })
61
- .catch(err => {
 
 
62
  console.error("Debt fetch failed:", err);
63
- el.textContent = "N/A";
64
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  }
66
 
 
67
  document.querySelectorAll("." + CLS).forEach(mount);
68
  })();
 
1
+ /* embed.js – live U.S.-debt counter */
2
  (() => {
3
+ const API = "https://ntdservices-debt-clock.hf.space/api/debt";
4
+ const CLS = "ntd-debt-clock";
5
+ const REFRESH_MS = 5 * 60 * 1000; // poll backend every 5 min
6
 
7
+ /* 1 β–Έ inject DSEG-7 font once per page */
8
  if (!document.getElementById("dseg-face")) {
9
  const style = document.createElement("style");
10
+ style.id = "dseg-face";
11
  style.textContent = `
12
  @font-face {
13
  font-family: "DSEG7";
14
+ src: url("https://cdn.jsdelivr.net/npm/@fontsource/dseg7-classic/files/dseg7-classic-latin-700-normal.woff2")
15
+ format("woff2");
16
  font-weight: 700;
17
  font-style: normal;
18
  font-display: swap;
19
+ }`;
 
20
  document.head.appendChild(style);
21
  }
22
 
23
+ /* 2 β–Έ mount one clock per matching element */
24
  function mount(el) {
25
  Object.assign(el.style, {
26
  font: "700 4rem 'DSEG7', monospace",
27
  color: "#ffb400",
28
+ textShadow: "0 0 8px rgba(255,180,0,.9)",
29
  background: "transparent",
30
  padding: "10px 18px",
31
  borderRadius: "6px",
32
  whiteSpace: "nowrap",
33
  letterSpacing: ".06em",
 
 
 
 
34
  userSelect: "none",
35
+ position: "relative",
36
  });
37
 
38
+ let baseDebt = 0; // dollars at baseTime
39
+ let ratePerSec = 0; // dollars / second
40
+ let baseTime = Date.now(); // ms clock when we set baseDebt
 
 
 
 
 
 
 
 
 
 
 
41
 
42
+ /* ── fetch latest figures ───────────────────────── */
43
+ async function pull() {
44
+ try {
45
+ const d = await fetch(API).then(r => r.json());
46
+ baseDebt = d.startingDebt;
47
+ ratePerSec = d.ratePerSecond;
48
+ baseTime = Date.now(); // reset integration point
49
+ } catch (err) {
50
  console.error("Debt fetch failed:", err);
51
+ }
52
+ }
53
+
54
+ /* initial fetch + periodic refresh */
55
+ pull(); // immediately
56
+ setInterval(pull, REFRESH_MS);
57
+
58
+ /* ── draw loop (runs each animation frame) ─────── */
59
+ function tick() {
60
+ const now = Date.now();
61
+ const delta = (now - baseTime) / 1000; // seconds elapsed
62
+ const debt = baseDebt + ratePerSec * delta;
63
+
64
+ el.textContent =
65
+ "$" + Math.round(debt).toLocaleString("en-US");
66
+
67
+ requestAnimationFrame(tick);
68
+ }
69
+ requestAnimationFrame(tick);
70
  }
71
 
72
+ /* auto-mount every <div class="ntd-debt-clock"> */
73
  document.querySelectorAll("." + CLS).forEach(mount);
74
  })();