Runtimes
Browser
requestAnimationFrame clock using performance.now.
createBrowserClock() targets display-driven environments (browsers, Electron renderer, etc.) where requestAnimationFrame exists.
Export
function createBrowserClock(): ClockFrom @watchstop/core.
Behavior
| Method | Implementation |
|---|---|
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, andperformance.now. - If
requestAnimationFrameorcancelAnimationFrameis missing (not a function),createBrowserClock()throws. - Do not import
node:modules. schedulemust not callcbsynchronously.
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 usesperformance.now(), not frame count.