Skip to content

useTimeout

类别
导出大小
403 B
最近修改
2 days ago

在给定时间后变为 true 的响应式值。

🌐 Reactive value that becomes true after a given time.

示例

Ready: false

用法

🌐 Usage

ts
import { 
useTimeout
} from '@vueuse/core'
const
ready
=
useTimeout
(1000)

1秒后,ready.value 变为 true

🌐 After 1 second, ready.value becomes true.

带控制

🌐 With Controls

ts
import { 
useTimeout
} from '@vueuse/core'
const {
ready
,
start
,
stop
,
isPending
} =
useTimeout
(1000, {
controls
: true })
// Check if timeout is pending
console
.
log
(
isPending
.
value
) // true
// Stop the timeout
stop
()
// Start/restart the timeout
start
()

选项

🌐 Options

选项类型默认值描述
controlsbooleanfalse暴露 startstopisPending 控件
immediatebooleantrue立即启动超时
callback() => void超时完成时调用

超时回调

🌐 Callback on Timeout

ts
import { 
useTimeout
} from '@vueuse/core'
useTimeout
(1000, {
callback
: () => {
console
.
log
('Timeout completed!')
}, })

反应间隔

🌐 Reactive Interval

超时时间可以是响应式的:

🌐 The timeout duration can be reactive:

ts
import { 
useTimeout
} from '@vueuse/core'
const
duration
=
ref
(1000)
const
ready
=
useTimeout
(
duration
)
// Change the duration (only affects future timeouts when using controls)
duration
.
value
= 2000