Skip to content

useInterval

Category
Export Size
457 B
Last Changed
10 months ago

Reactive counter that increases on every interval.

Demo

Interval fired: 10

Usage

ts
import { 
useInterval
} from '@vueuse/core'
// count will increase every 200ms const
counter
=
useInterval
(200)

With Controls

ts
import { 
useInterval
} from '@vueuse/core'
const {
counter
,
reset
,
pause
,
resume
,
isActive
} =
useInterval
(200, {
controls
: true,
}) // Reset counter to 0
reset
()
// Pause/resume the interval
pause
()
resume
()

Options

OptionTypeDefaultDescription
controlsbooleanfalseExpose pause, resume, reset, and isActive controls
immediatebooleantrueStart the interval immediately
callback(count: number) => voidCalled on every interval with the current count

Reactive Interval

The interval can be reactive:

ts
import { 
useInterval
} from '@vueuse/core'
const
intervalMs
=
ref
(1000)
const
counter
=
useInterval
(
intervalMs
)
// Change the interval dynamically
intervalMs
.
value
= 500

Callback on Every Interval

ts
import { 
useInterval
} from '@vueuse/core'
useInterval
(1000, {
callback
: (
count
) => {
console
.
log
(`Tick ${
count
}`)
}, })

Type Declarations

ts
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
:
ShallowRef
<number>
reset
: () => void
} export type
UseIntervalReturn
=
|
Readonly
<
ShallowRef
<number>>
|
Readonly
<UseIntervalControls &
Pausable
>
/** * Reactive counter increases on every interval * * @see https://vueuse.org/useInterval * @param interval * @param options */ export declare function
useInterval
(
interval
?:
MaybeRefOrGetter
<number>,
options
?:
UseIntervalOptions
<false>,
):
Readonly
<
ShallowRef
<number>>
export declare function
useInterval
(
interval
:
MaybeRefOrGetter
<number>,
options
:
UseIntervalOptions
<true>,
):
Readonly
<UseIntervalControls &
Pausable
>

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
IlyaL
Jelf
SerKo
Robin
mohammadhosry
Waleed Khaled
vaakian X
Nurettin Kaya

Changelog

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)

Released under the MIT License.

2 for 1 SALE
Free JavaScript Certification with any Certification purchased
Get Certified
04
hours
:
19
minutes
:
48
seconds
: