useResettableTimeout

Restartable timeout controls with explicit start and clear methods.

Use useResettableTimeout to schedule a single timeout that resets on repeated starts.

Usage

import { useResettableTimeout } from "@tilt-legal/cubitt-components/utilities/hooks";

const { start, clear } = useResettableTimeout(() => {
  setOpen(false);
}, 300);

start(); // schedules callback in 300ms
start(500); // resets and schedules for 500ms
clear(); // cancels pending callback

API

Parameters

OptionTypeDescriptionDefault
callback() => voidFunction called when the timeout fires-
delaynumberTimeout duration in milliseconds-

Return Value

ValueTypeDescription
start(delayOverride?: number) => voidStarts the timeout and resets any active timer
clear() => voidCancels the currently scheduled timeout

Behavior

  • start always clears any pending timer before creating a new one.
  • The latest callback reference is used when the timeout fires.
  • Pending timers are cleaned up on unmount.

On this page