Skip to content
Source
Sign inSign up

tickTextContent.ts

18 lines665 Bytes

1import { secondTick } from "@t3tools/client-runtime/clock";
2​
3/**
4 * Ref callback that writes `format(Date.now())` into a node and, while `live`,
5 * rewrites it on the shared second tick. Running timers update through the DOM
6 * so a ticking label never re-renders the timeline or composer around it.
7 */
8export function tickTextContent(format: (nowMs: number) => string, live: boolean) {
9 return (node: HTMLElement | null) => {
10 if (!node) return;
11 const write = () => {
12 const text = format(Date.now());
13 if (node.textContent !== text) node.textContent = text;
14 };
15 write();
16 return live ? secondTick.subscribe(write) : undefined;
17 };