Skip to main content

@rtorcato/browser-common / idle

idle

Functions​

isIdleDetectionApiAvailable()​

isIdleDetectionApiAvailable(): boolean

Defined in: idle/index.ts:10

Checks if the Idle Detection API is available in the browser.

Example​

import { isIdleDetectionApiAvailable } from '@rtorcato/browser-common/idle'
if (isIdleDetectionApiAvailable()) startIdleTracking()

Returns​

boolean


onIdle()​

onIdle(callback, options?): number

Defined in: idle/index.ts:27

Registers a callback to be called when the browser is idle (using requestIdleCallback). Falls back to setTimeout if not available.

Example​

import { onIdle, cancelIdle } from '@rtorcato/browser-common/idle'
const id = onIdle((deadline) => doWork(deadline))
cancelIdle(id)

Parameters​

callback​

(deadline?) => void

The function to call when idle.

options?​

IdleRequestOptions

Optional options for requestIdleCallback.

Returns​

number

The callback ID (for cancellation).


cancelIdle()​

cancelIdle(id): void

Defined in: idle/index.ts:47

Cancels an idle callback registered with onIdle.

Example​

import { cancelIdle } from '@rtorcato/browser-common/idle'
cancelIdle(id)

Parameters​

id​

number

The callback ID returned by onIdle.

Returns​

void


detectIdle()​

detectIdle(onIdle, onActive?): Promise<IdleDetector | null>

Defined in: idle/index.ts:83

Uses the Idle Detection API to run a callback when the user/system is idle. Requires permissions and a secure context.

Example​

import { detectIdle } from '@rtorcato/browser-common/idle'
const detector = await detectIdle(() => lockUI(), () => unlockUI())

Parameters​

onIdle​

() => void

Callback when idle.

onActive?​

() => void

Optional callback when user becomes active.

Returns​

Promise<IdleDetector | null>

The IdleDetector instance, or null if not available.