Docs
useRandomInterval
Execute a callback function at a random interval with useRandomInterval.
Installation
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-random-interval
Usage
"use client"
import * as React from "react"
import { useRandomInterval } from "@/hooks/use-random-interval"
export function Component() {
const [frame, setFrame] = React.useState(0)
const clear = useRandomInterval(() => setFrame((prev) => prev + 1), {
minDelay: 100,
maxDelay: 1000,
})
return (
<div>
<p>Animation Frame: {frame}</p>
<button onClick={clear}>Stop Animation</button>
</div>
)
}
API Reference
Parameters
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
cb | () => void | Callback function to execute at random intervals | - | No |
options | { minDelay: number, maxDelay: number } | Configuration object with delay range | {} | Yes |
Return Values
Type | Description |
---|---|
() => void | Cleanup function to stop the random interval execution |