@supunlakmal/hooks
Version:
A collection of reusable React hooks
18 lines (17 loc) • 1.02 kB
TypeScript
/**
* A custom hook similar to `setInterval`, but the interval only runs when a specific
* condition (`when`) is true. It clears the interval automatically on unmount or
* when the `delay` or `when` condition changes appropriately.
*
* @param {() => void} callback The function to execute at each interval.
* @param {number | null | undefined} delay The interval duration in milliseconds.
* The interval is paused if `delay` is `null` or `undefined`, or if `when` is `false`.
* @param {object} [options] Optional configuration options.
* @param {boolean} [options.when=true] A boolean condition. The interval runs only when this is true.
* @param {boolean} [options.startImmediate=false] If true, the callback is executed immediately
* when the interval starts or resumes (i.e., when `when` becomes true and `delay` is valid).
*/
export declare const useIntervalWhen: (callback: () => void, delay: number | null | undefined, options?: {
when?: boolean;
startImmediate?: boolean;
}) => void;