useInterval ​
Hook that makes and interval and returns controlling functions
time
test coverage
Last changed: last month
Installation ​
Library
CLI
Manual
typescript
import { useInterval } from '@siberiacancode/reactuse';
Usage ​
typescript
const { active, pause, resume, toggle } = useInterval(() => console.log('inside interval'), 2500);
// or
const { active, pause, resume, toggle } = useInterval(() => console.log('inside interval'), { interval: 2500 });
Demo ​
Api ​
Parameters
Name | Type | Default | Note |
---|---|---|---|
callback | () => void | - | Any callback function |
interval? | number | 1000 | Time in milliseconds |
options.immediately? | boolean | true | Start the interval immediately |
Returns
UseIntervalReturn
Parameters
Name | Type | Default | Note |
---|---|---|---|
callback | () => void | - | Any callback function |
options.interval? | number | 1000 | Time in milliseconds |
options.immediately? | boolean | true | Start the interval immediately |
Type declaration ​
typescript
export interface UseIntervalOptions {
/** Start the interval immediately */
immediately?: boolean;
}
export interface UseIntervalReturn {
/** Is the interval active */
active: boolean;
/** Pause the interval */
pause: () => void;
/** Resume the interval */
resume: () => void;
/** Toggle the interval */
toggle: () => void;
}
interface UseInterval {
(callback: () => void, interval?: number, options?: UseIntervalOptions): UseIntervalReturn;
(callback: () => void, options?: UseIntervalOptions & { interval?: number }): UseIntervalReturn;
}
Source ​
Source • DemoContributors ​
D
K