idle-timeout
v2.4.0
Published
The minimal idle state detector.
Maintainers
Readme
idleTimeout
A zero dependency, ~3KB library that makes idle state detection in the browser easy. With its simple but powerful API, it includes everything you need.
Installation
Using npm
npm install idle-timeoutUsing pnpm
pnpm add idle-timeoutUsing yarn
yarn add idle-timeoutUsing CDN
<script src="https://unpkg.com/idle-timeout/dist/idle-timeout.min.umd.js"></script>Usage
idleTimeout is easy to use:
import idleTimeout from 'idle-timeout';
idleTimeout(() => {
console.log('Idle state detected.');
});Documentation
The idleTimeout constructor takes two arguments:
callback [Function]- The callback function (receiveselementandtimeoutas arguments)element [Document | HTMLElement]- The element being monitored for activitytimeout [Number]- The current timeout value in milliseconds
options [Object]- An optional options objectelement [Document | HTMLElement]- The element to monitor for activitytimeout [Number]- The idle timeout (in milliseconds)loop [Boolean]- Whether the timeout should be looped when idle
const instance = idleTimeout(
(element, timeout) => {
console.log(`Idle state detected on ${element} after ${timeout} ms`);
},
{
element: document,
timeout: 1000 * 60 * 5,
loop: false
}
);Methods
pause() - Pauses the timeout
instance.pause();resume() - Resumes a paused timeout
instance.resume();reset() - Resets the timeout
instance.reset();destroy() - Destroys the instance
instance.destroy();Getters
idle [Boolean] - Whether the current state is idle
instance.idle; // falseSetters
timeout = value [Number] - Sets the timeout (in milliseconds)
instance.timeout = 1000 * 60;loop = value [Boolean] - Set whether the timeout should be looped
instance.loop = true;idle = value [Boolean] - Set the idle state
instance.idle = true;License
This project is licensed under the terms of the MIT License.
