Skip to content
On this page

useInterval

Category
Export Size
638 B
Last Changed
1 hour ago

Reactive counter increases on every interval

Demo

Interval fired: 0

Usage

js
import { useInterval } from '@vueuse/core'

// count will increase every 200ms
const counter = useInterval(200)
import { useInterval } from '@vueuse/core'

// count will increase every 200ms
const counter = useInterval(200)
ts
const { counter, reset, pause, resume } = useInterval(200, { controls: true })
const { counter, reset, pause, resume } = useInterval(200, { controls: true })

Type Declarations

typescript
export interface UseIntervalOptions<Controls extends boolean> {
  /**
   * Expose more controls
   *
   * @default false
   */
  controls?: Controls
  /**
   * Execute the update immediately on calling
   *
   * @default true
   */
  immediate?: boolean
  /**
   * Callback on every interval
   */
  callback?: (count: number) => void
}
export interface UseIntervalControls {
  counter: Ref<number>
  reset: () => void
}
/**
 * Reactive counter increases on every interval
 *
 * @see https://vueuse.org/useInterval
 * @param interval
 * @param options
 */
export declare function useInterval(
  interval?: MaybeComputedRef<number>,
  options?: UseIntervalOptions<false>
): Ref<number>
export declare function useInterval(
  interval: MaybeComputedRef<number>,
  options: UseIntervalOptions<true>
): UseIntervalControls & Pausable
export interface UseIntervalOptions<Controls extends boolean> {
  /**
   * Expose more controls
   *
   * @default false
   */
  controls?: Controls
  /**
   * Execute the update immediately on calling
   *
   * @default true
   */
  immediate?: boolean
  /**
   * Callback on every interval
   */
  callback?: (count: number) => void
}
export interface UseIntervalControls {
  counter: Ref<number>
  reset: () => void
}
/**
 * Reactive counter increases on every interval
 *
 * @see https://vueuse.org/useInterval
 * @param interval
 * @param options
 */
export declare function useInterval(
  interval?: MaybeComputedRef<number>,
  options?: UseIntervalOptions<false>
): Ref<number>
export declare function useInterval(
  interval: MaybeComputedRef<number>,
  options: UseIntervalOptions<true>
): UseIntervalControls & Pausable

Source

SourceDemoDocs

Contributors

Anthony Fu
Jelf
mohammadhosry
Waleed Khaled
vaakian X
Nurettin Kaya

Changelog

v9.13.0 on 2/18/2023
db66c - fix: fix types
81db6 - feat: add reset function (#2686)
v9.3.0 on 9/26/2022
33dbc - feat: optional callback for useInterval, useTimeout & useTimestamp (#2240)
v9.0.0-beta.1 on 7/20/2022
43732 - fix!: rename type IntervalOptions to UseIntervalOptions (#1945)
v8.9.1 on 7/8/2022
a9ccc - feat(all): use MaybeComputedRef (#1768)
v7.6.0 on 2/8/2022
a2b2b - feat: support ref as interval (#1215)

Released under the MIT License.