shadcn-ahooks

useInterval

A hook that handles the setInterval timer function.

Overview

A hook that handles the setInterval timer function.

Documentation and Examples

Installation

Open in
pnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInterval.json
npx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInterval.json
yarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInterval.json
bun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInterval.json

A hook that handles the setInterval timer function.

Examples

Default usage

Advanced usage

API

useInterval(
  fn: () => void,
  delay?: number | undefined,
  options?: Options
): fn: () => void;

Params

PropertyDescriptionType
fnThe function to be executed every delay milliseconds.() => void
delayThe time in milliseconds, the timer should delay in between executions of the specified function. The timer will be cancelled if delay is set to undefined.number | undefined
optionsConfig of the interval behavior.Options

Options

PropertyDescriptionTypeDefault
immediateWhether the function should be executed immediately on first executionbooleanfalse

Result

PropertyDescriptionType
clearIntervalclear interval() => void

On this page