# hooks/useClock (zidane-ui)

Import: `import { … } from 'zidane-ui'`. Source: [ui/src/hooks/useClock.ts](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/useClock.ts).

## Functions

### `useClock`

Function.

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

```ts
function useClock(intervalMs: number, active?: boolean): number
```

**Parameters**

- `intervalMs` (`number`): How often: `60_000` for relative times, `1000` for countdowns.
- `active?` (`boolean`, default `true`): Whether anything on screen moves. Off: no interval, no re-render.

**Returns** `number`: The time to measure from: the last tick's, or when the caller started reading when that is later. Under a
[`ClockStartContext`](https://zidane.si/docs/reference/ui-hooks-use-clock.md#ClockStartContext), its start until the clock's first tick after the page is hydrated.

Source: [ui/src/hooks/useClock.ts:121](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/useClock.ts#L121)

### `useClockReader`

Function.

Reads the time on the clock [`useClock`](https://zidane.si/docs/reference/ui-hooks-use-clock.md#useClock) answers with, whenever it is called: the page's under a
[`ClockStartContext`](https://zidane.si/docs/reference/ui-hooks-use-clock.md#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.

```ts
function useClockReader(): () => number
```

**Returns** `() => number`: The reader. A new one once the page is hydrated: a timer started with the old one starts again.

Source: [ui/src/hooks/useClock.ts:102](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/useClock.ts#L102)

## Constants

### `ClockStartContext`

Constant.

The time a server drew the page at, in ms: what [`useClock`](https://zidane.si/docs/reference/ui-hooks-use-clock.md#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.

```ts
const ClockStartContext: Context<number | null>
```

Source: [ui/src/hooks/useClock.ts:24](https://github.com/Tahul/zidane/blob/main/ui/src/hooks/useClock.ts#L24)
