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.

Browser supportโ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
IdleDetectorโœ… 94+โœ… 114+โŒโŒโŒLimited
Window.requestIdleCallbackโœ… 47+โœ… 79+โœ… 55+โŒ flag onlyโŒLimited

โœ… supported ยท โ— partial ยท โŒ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: IdleDetector ยท Window.requestIdleCallback

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.