use-interval
shadcn-hooks/use-intervalFreeHook
A hook that creates an interval.
Install it
npx shadcn@latest add https://shadcn-hooks.com/r/use-interval.jsonSource
1import { useCallback, useEffect, useRef } from 'react'2import { useMemoizedFn } from '@/registry/hooks/use-memoized-fn'34export function useInterval(5 fn: () => void,6 delay?: number,7 options?: { immediate?: boolean },8) {9 const fnRef = useMemoizedFn(fn)10 const timerRef = useRef<number | null>(null)1112 const clear = useCallback(() => {13 if (timerRef.current) {14 clearInterval(timerRef.current)15 timerRef.current = null16 }17 }, [])1819 useEffect(() => {20 if (!(typeof delay === 'number') || delay < 0) {21 return22 }2324 if (options?.immediate) {25 fnRef()26 }2728 timerRef.current = window.setInterval(fnRef, delay)2930 return () => {31 clear()32 }33 }, [delay, options?.immediate, fnRef, clear])3435 return clear36}
What it pulls in
Other registry items
Files it writes
More from shadcn-hooks
All 58 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| use-active-elementuse-active-element | shadcn-hooks | Hooks | Free | no deps | |
| use-batteryuse-battery | shadcn-hooks | Hooks | Free | no deps | |
| use-booleanuse-boolean | shadcn-hooks | Hooks | Free | no deps | |
| use-click-any-whereuse-click-any-where | shadcn-hooks | Hooks | Free | no deps | |
| use-click-awayuse-click-away | shadcn-hooks | Hooks | Free | no deps | |
| use-clipboarduse-clipboard | shadcn-hooks | Hooks | Free | no deps | |
| use-controllable-valueuse-controllable-value | shadcn-hooks | Hooks | Free | 1 dep | |
| use-counteruse-counter | shadcn-hooks | Hooks | Free | no deps |
