Implementation:AIScientists Dev WorldSeed usePausableTimer
| Knowledge Sources | |
|---|---|
| Domains | Frontend, Timing |
| Last Updated | 2026-10-26 18:00 GMT |
Overview
Concrete React hook providing a pausable setTimeout that freezes on pause and resumes with remaining time, provided by the WorldSeed frontend.
Description
The usePausableTimer hook returns `start(ms)` and `clear()` functions. When `paused` is true, the timer freezes and stores remaining time. When unpaused, it re-schedules with the remaining duration. Only one timer is active at a time — calling `start` again replaces the previous. The hook uses refs internally to avoid stale closures and cleans up on unmount. It is used by `OverlayBubble` and `NarrativeBar` for hold timers that must respect the world's pause state.
Usage
Import this hook in components that need timed transitions which should freeze when the world is paused — e.g., subtitle bubble hold durations or narrative bar display timers.
Code Reference
Source Location
- Repository: WorldSeed
- File: frontend/src/hooks/usePausableTimer.ts
Signature
export function usePausableTimer(onFire: () => void, paused: boolean): {
start: (ms: number) => void;
clear: () => void;
};
Import
import { usePausableTimer } from '@/hooks/usePausableTimer';
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| onFire | () => void | Yes │ Callback when timer fires | |
| paused | boolean | Yes │ Whether to freeze the timer | |
| ms | number | Yes (start) │ Duration in milliseconds |
Outputs
| Name | Type | Description |
|---|---|---|
| start | function │ Starts a pausable timer | |
| clear | function │ Clears the active timer |
Usage Examples
import { usePausableTimer } from '@/hooks/usePausableTimer';
function OverlayBubble({ worldPaused }) {
const { start, clear } = usePausableTimer(() => {
// Advance to next subtitle
}, worldPaused);
// Start a 3-second hold; freezes when world pauses
start(3000);
}