Skip to content

useIdle

Category
Export Size
1.18 kB
Last Changed
last month

Tracks whether the user is being inactive.

Demo

For demonstration purpose, the idle timeout is set to 5s in this demo (default 1min).
Idle: false
Inactive: 2s

Usage

ts
import { 
useIdle
} from '@vueuse/core'
const {
idle
,
lastActive
} =
useIdle
(5 * 60 * 1000) // 5 min
console
.
log
(
idle
.
value
) // true or false

Programatically resetting:

ts
import { 
useCounter
,
useIdle
} from '@vueuse/core'
import {
watch
} from 'vue'
const {
inc
,
count
} =
useCounter
()
const {
idle
,
lastActive
,
reset
} =
useIdle
(5 * 60 * 1000) // 5 min
watch
(
idle
, (
idleValue
) => {
if (
idleValue
) {
inc
()
console
.
log
(`Triggered ${
count
.
value
} times`)
reset
() // restarts the idle timer. Does not change lastActive value
} })

Component Usage

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

vue
<template>
  <UseIdle v-slot="{ 
idle
}"
:timeout
="5 * 60 * 1000">
Is Idle: {{
idle
}}
</UseIdle> </template>

Type Declarations

ts
export interface UseIdleOptions
  extends ConfigurableWindow,
    ConfigurableEventFilter {
  /**
   * Event names that listen to for detected user activity
   *
   * @default ['mousemove', 'mousedown', 'resize', 'keydown', 'touchstart', 'wheel']
   */
  
events
?:
WindowEventName
[]
/** * Listen for document visibility change * * @default true */
listenForVisibilityChange
?: boolean
/** * Initial state of the ref idle * * @default false */
initialState
?: boolean
} export interface UseIdleReturn extends Stoppable {
idle
:
ShallowRef
<boolean>
lastActive
:
ShallowRef
<number>
reset
: () => void
} /** * Tracks whether the user is being inactive. * * @see https://vueuse.org/useIdle * @param timeout default to 1 minute * @param options IdleOptions */ export declare function
useIdle
(
timeout
?: number,
options
?: UseIdleOptions,
): UseIdleReturn

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
Anthony Fu
wheat
dpbs
SerKo
Arthur Darkstone
Melkumyants Danila
IlyaL
Fernando Fernández
Intaek H
nahvego
Hawtim
vaakian X
Jelf
Dan Mindru
btea
Shinigami
Alex Kozack
Antério Vieira

Changelog

v14.0.0 on
3f96a - feat: implement Stoppable interface (#5068)
v14.0.0-alpha.0 on
8c521 - feat(components)!: refactor components and make them consistent (#4912)
v13.4.0 on
319d8 - feat(shared): Introduce TimerHandle for setTimeout type (#4801)
c6469 - fix: changed the reset call when the initial value is true (#4800)
v12.4.0 on
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)

Released under the MIT License.

Black Friday
Save up to 80% on your Vue.js upskilling
Claim Now
27
hours
:
03
minutes
:
23
seconds
: