Skip to main content

idle

Runs a callback when the browser or system is idle โ€” via requestIdleCallback (falling back to setTimeout) or the Chromium-only Idle Detection API.

Import: @rtorcato/browser-common/idle

๐Ÿ“– MDN: Idle Detection API ยท ๐Ÿ“Š caniuse: IdleDetector

The Idle Detection API (detectIdle) is Chromium-only, requires a permission grant and a secure context, and returns null when unavailable โ€” check with isIdleDetectionApiAvailable() first. onIdle/cancelIdle use requestIdleCallback and work everywhere via the setTimeout fallback.

Exampleโ€‹

import {
isIdleDetectionApiAvailable,
onIdle,
cancelIdle,
detectIdle,
} from '@rtorcato/browser-common/idle'

const id = onIdle((deadline) => doWork(deadline))
cancelIdle(id)

if (isIdleDetectionApiAvailable()) {
const detector = await detectIdle(
() => lockUI(),
() => unlockUI()
)
}

Exportsโ€‹

  • isIdleDetectionApiAvailable() โ€” feature check for the Idle Detection API
  • onIdle(callback, options?) โ€” schedules a callback via requestIdleCallback (or setTimeout fallback)
  • cancelIdle(id) โ€” cancels a callback registered with onIdle
  • detectIdle(onIdle, onActive?) โ€” starts the Idle Detection API, returns the detector or null

See the API reference for full signatures.