Skip to content
On this page

useInterval

每隔一段时间响应式增加计数

Reactive counter increases on every interval

例子

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

Category
Export Size
638 B
Last Changed
last month

SourceDemoDocs

贡献者(Contributors)

日志(Changelog)

v9.13.0 on 2023/2/18
db66c - fix: fix types
81db6 - feat: add reset function (#2686)

Released under the MIT License.