Skip to content
usefy

useInterval

Timing & async

A lightweight, type-safe React hook for declarative setInterval with automatic cleanup and start/stop controls

Install

$ npm install @usefy/use-interval

Quick start

useInterval.tsx
import { useState } from "react";
import { useInterval } from "@usefy/use-interval";

function Clock() {
  const [time, setTime] = useState(() => new Date());

  useInterval(() => {
    setTime(new Date());
  }, 1000);

  return <div>{time.toLocaleTimeString()}</div>;
}

API reference

useInterval(callback, delay, options?)

Runs callback every delay milliseconds until unmount or stopped.

Parameters

ParameterTypeDescription
callback() => voidFunction to run on each tick (always the latest reference)
delaynumber | null | undefinedInterval in ms, or null/undefined to disable
optionsUseIntervalOptions{ immediate?: boolean; autoStart?: boolean }

Options

OptionTypeDefaultDescription
immediatebooleanfalseRun the callback immediately on (re)start, then at each interval
autoStartbooleantrueStart automatically on mount; when false, call start()

Returns UseIntervalReturn

PropertyTypeDescription
start() => voidStart the interval (idempotent while already running)
stop() => voidStop the interval (idempotent while already stopped)
toggle() => voidToggle between running and stopped
isRunningbooleanWhether the interval is ticking (started and valid delay)

Go deeper

This page is the quick reference. For every example, prop, and edge case, read the full README — or open Storybook to change props live.

More in timing & async