Appearance
useCountDown
一个用于管理倒计时的 Hook
Demo
</>
countdown: 0
Usage
import { useCountDown } from 'v3-use'
const [timeLeft, { start, pause, play, stop }] = useCountDown({
onEnd: () => {
console.log('End of the time')
},
interval: 500,
step: 1,
})
</script>
import { useCountDown } from 'v3-use'
const [timeLeft, { start, pause, play, stop }] = useCountDown({
onEnd: () => {
console.log('End of the time')
},
interval: 500,
step: 1,
})
</script>
Reference
const [timeLeft, actions] = useCountDown<T>(options?: Options)
const [timeLeft, actions] = useCountDown<T>(options?: Options)
Type Declarations
export declare type Options<T> = {
defaultTimeLeft?: number
interval?: number
step?: number
format?: (left: number) => T
onEnd?: () => void
}
export declare type Actions = {
start: (t?: number) => void
play: () => void
pause: () => void
stop: () => void
}
export declare function useCountDown<T>(options?: Options<T>): [Ref<T>, Actions]
export declare type Options<T> = {
defaultTimeLeft?: number
interval?: number
step?: number
format?: (left: number) => T
onEnd?: () => void
}
export declare type Actions = {
start: (t?: number) => void
play: () => void
pause: () => void
stop: () => void
}
export declare function useCountDown<T>(options?: Options<T>): [Ref<T>, Actions]
Params
defaultTimeLeft
: number
— 默认剩余时间 默认值0
interval
: number
— 循环间隔(ms),默认值1000
step
: number
— 间隔尺寸,默认值1
format
: (left: number) => T
— 剩余时长格式化函数,默认值undefined
onEnd
: () => void
— 结束钩子,默认值undefined
Result
state
: Ref<T>
— 剩余时间actions
: Actions
— 操作集合
Actions
start
: (t?: number) => void
— 初始化倒计时函数,可接收初始倒计时间,默认defaultTimeLeft
pause
: () => void
— 暂停倒计时函数play
: () => void
— 继续倒计时函数stop
: () => void
— 终止倒计时函数,倒计时归0