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 APIonIdle(callback, options?)โ schedules a callback viarequestIdleCallback(orsetTimeoutfallback)cancelIdle(id)โ cancels a callback registered withonIdledetectIdle(onIdle, onActive?)โ starts the Idle Detection API, returns the detector ornull
See the API reference for full signatures.