useInterval
A hook that handles the setInterval timer function.
Overview
A hook that handles the setInterval timer function.
Installation
Open inpnpm dlx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInterval.jsonnpx shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInterval.jsonyarn shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInterval.jsonbun shadcn@latest add https://shadcn-ahooks.vercel.app/r/useInterval.jsonA hook that handles the setInterval timer function.
Examples
Default usage
Advanced usage
API
useInterval(
fn: () => void,
delay?: number | undefined,
options?: Options
): fn: () => void;Params
| Property | Description | Type |
|---|---|---|
| fn | The function to be executed every delay milliseconds. | () => void |
| delay | The 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 |
| options | Config of the interval behavior. | Options |
Options
| Property | Description | Type | Default |
|---|---|---|---|
| immediate | Whether the function should be executed immediately on first execution | boolean | false |
Result
| Property | Description | Type |
|---|---|---|
| clearInterval | clear interval | () => void |
useInfiniteScroll
useInfiniteScroll encapsulates the common infinite scroll logic.
useInViewport
Observe whether the element is in the visible area, and the visible area ratio of the element. More information refer to [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).