Watchstop
Runtimes

Browser

requestAnimationFrame clock using performance.now.

createBrowserClock() targets display-driven environments (browsers, Electron renderer, etc.) where requestAnimationFrame exists.

Export

function createBrowserClock(): Clock

From @watchstop/core.

Behavior

MethodImplementation
now()performance.now()
schedule(cb)requestAnimationFrame(() => cb()) — return the numeric/frame handle
cancel(handle)cancelAnimationFrame(handle) (idempotent wrapper)

Requirements

  • Requires a DOM-like global with requestAnimationFrame, cancelAnimationFrame, and performance.now.
  • If requestAnimationFrame or cancelAnimationFrame is missing (not a function), createBrowserClock() throws.
  • Do not import node: modules.
  • schedule must not call cb synchronously.

Usage

import { Stopwatch, createBrowserClock } from '@watchstop/core'

const sw = new Stopwatch(createBrowserClock())
sw.start()

Prefer new Stopwatch() / detectClock() when you want automatic selection. Use createBrowserClock() when you explicitly need rAF even if other clocks are available.

Notes

  • Tick rate follows the display refresh (typically ~60 Hz). That is intentional for UI stopwatches.
  • In background tabs, browsers may throttle rAF; elapsed from get() stays correct because it uses performance.now(), not frame count.

On this page