Skip to content

useTimeout

Category
Export Size
403 B
Last Changed
4 months ago

Reactive value that becomes true after a given time.

Demo

Ready: true

Usage

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

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

OptionTypeDefaultDescription
controlsbooleanfalseExpose start, stop, and isPending controls
immediatebooleantrueStart the timeout immediately
callback() => voidCalled when the timeout completes

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

Type Declarations

ts
export interface 
UseTimeoutOptions
<
Controls
extends boolean,
> extends UseTimeoutFnOptions { /** * Expose more controls * * @default false */
controls
?:
Controls
/** * Callback on timeout */
callback
?:
Fn
} export type
UseTimeoutReturn
=
|
ComputedRef
<boolean>
| ({ readonly
ready
:
ComputedRef
<boolean>
} &
Stoppable
)
/** * @deprecated use UseTimeoutReturn instead */ export type
UseTimoutReturn
=
UseTimeoutReturn
/** * Update value after a given time with controls. * * @see {@link https://vueuse.org/useTimeout} * @param interval * @param options */ export declare function
useTimeout
(
interval
?:
MaybeRefOrGetter
<number>,
options
?:
UseTimeoutOptions
<false>,
):
ComputedRef
<boolean>
export declare function
useTimeout
(
interval
:
MaybeRefOrGetter
<number>,
options
:
UseTimeoutOptions
<true>,
): {
ready
:
ComputedRef
<boolean>
} &
Stoppable

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
Vida Xie
Rainbow
NoiseFan
SerKo
Robin
IlyaL
OrbisK
huiliangShen
Waleed Khaled
vaakian X
Jelf
Shinigami
Mark Noonan

Changelog

v14.1.0 on
31e5c - fix: fix type typo (#5147)
v13.1.0 on
c1d6e - feat(shared): ensure return types exists (#4659)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.10.0 on
4eede - feat: target support reactivity (#3923)

Released under the MIT License.

Ship Faster with AI Dev Workflows
Flagship advanced AI course is coming soon
Join Waitlist