Skip to content

useCountdown

useIntervalFn 的封装器,提供倒计时器。

¥Wrapper for useIntervalFn that provides a countdown timer.

示例

🚀
Rocket launch in 5 seconds
Countdown:

用法

¥Usage

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

const countdownSeconds = 5
const { remaining, start, stop, pause, resume } = useCountdown(countdownSeconds, {
  onComplete() {

  },
  onTick() {

  }
})

类型声明

显示类型声明
typescript
export interface UseCountdownOptions {
  /**
   *  Interval for the countdown in milliseconds. Default is 1000ms.
   */
  interval?: MaybeRefOrGetter<number>
  /**
   * Callback function called when the countdown reaches 0.
   */
  onComplete?: () => void
  /**
   * Callback function called on each tick of the countdown.
   */
  onTick?: () => void
  /**
   * Start the countdown immediately
   *
   * @default false
   */
  immediate?: boolean
}
export interface UseCountdownReturn extends Pausable {
  /**
   * Current countdown value.
   */
  remaining: Ref<number>
  /**
   * Resets the countdown and repeatsLeft to their initial values.
   */
  reset: () => void
  /**
   * Stops the countdown and resets its state.
   */
  stop: () => void
  /**
   * Reset the countdown and start it again.
   */
  start: (initialCountdown?: MaybeRefOrGetter<number>) => void
}
/**
 * Wrapper for `useIntervalFn` that provides a countdown timer in seconds.
 *
 * @param initialCountdown
 * @param options
 *
 * @see https://vueuse.org/useCountdown
 */
export declare function useCountdown(
  initialCountdown: MaybeRefOrGetter<number>,
  options?: UseCountdownOptions,
): UseCountdownReturn

源代码

源代码示例文档

变更日志

v12.5.0 on 1/22/2025
69ced - feat: new function (#4125)

VueUse 中文网 - 粤ICP备13048890号