Skip to content

hooks/useClock

import { … } from 'zidane-ui'ui/src/hooks/useClock.ts

Functions#

useClockFunction#

Re-renders the caller every intervalMs while active (and when the window shows again). Every reader of one length shares one interval.

TypeScript
function useClock(intervalMs: number, active?: boolean): number
Parameters
NameTypeDefaultDescription
intervalMsnumber

How often: 60_000 for relative times, 1000 for countdowns.

active?booleantrue

Whether anything on screen moves. Off: no interval, no re-render.

number

The time to measure from: the last tick's, or when the caller started reading when that is later. Under a ClockStartContext, its start until the clock's first tick after the page is hydrated.

ui/src/hooks/useClock.ts:121

useClockReaderFunction#

Reads the time on the clock useClock answers with, whenever it is called: the page's under a ClockStartContext (its start until the page is hydrated), else the machine's. For a time read once (when a card opened) or on a component's own timer.

TypeScript
function useClockReader(): () => number
() => number

The reader. A new one once the page is hydrated: a timer started with the old one starts again.

ui/src/hooks/useClock.ts:102

Constants#

ClockStartContextConstant#

The time a server drew the page at, in ms: what useClock answers on that server and while the browser hydrates its HTML (from then on, the machine's clock: what happens on the page is stamped by it). The page sends it with its markup and provides the same value on both sides. null (the default): the machine's clock.

TypeScript
const ClockStartContext: Context<number | null>

ui/src/hooks/useClock.ts:24