Skip to content

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

NameTypeDefaultNote
callback() => void-Any callback function
interval?number1000Time in milliseconds
options.immediately?booleantrueStart the interval immediately

Returns

UseIntervalReturn

Parameters

NameTypeDefaultNote
callback() => void-Any callback function
options.interval?number1000Time in milliseconds
options.immediately?booleantrueStart 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 • Demo

Contributors ​

D
debabin
debabin
K
khmilevoi
khmilevoi

Released under the MIT License.