Giter Site home page Giter Site logo

idle-timer's Introduction

idle-timer

A timer to detect lapses in browser activity

The initial implementation is based on jquery-idletimer, but rewritten to break the dependency on jQuery, and to take advantage of modern JavaScript.

Synopsis

The browser is considered "idle", if within a specified period of time:

  • The mouse has not moved
  • The mouse wheel has not scrolled
  • No key has been pressed

When such an "idle" state is detected, an event is dispatched on the element on which the idle timer has been registered.

Usage

import IdleTimer from 'idle-timer';

let handle_idle_timer_event = function(event) {
    const detail = event.detail;
    const timer = detail.timer;
    const original_event = detail.event; /* N.B. May be null */
    console.log('timer: %s (%s)',
                event.type,
                (original_event ? original_event.type : null));
};

console.log('timer: setup');
let timer = new IdleTimer({timeout: 2000, element: window.document});
window.document.addEventListener('idle-timer:idle', handle_idle_timer_event);
window.document.addEventListener('idle-timer:active', handle_idle_timer_event);

API: Constructor options

element

Defaulting to document, this indicates the element which is to be monitored for activity.

initially_idle

Defaulting to false, this indicates whether the instance should be considered "idle" initially.

timeout

Defaulting to 30000, this indicates the duration in milliseconds after which the "idle" state is triggered.

events

This indicates the array of events to be monitored, and defaults to the following value.

[
    'mousemove',
    'keydown',
    'wheel',
    'DOMMouseScroll',
    'mousewheel',
    'mousedown'
]

storage_key

Defaulting to null, this indicates a localStorage key to be used for synchronising the timer between multiple browser tabs/windows.

API: Timer management methods

Clean up a timer that is no longer required:

timer.destroy();

Reset a timer to its initial conditions:

timer.reset();

Pause a timer:

timer.pause();

Resume a previously-paused timer:

timer.resume();

API: Query methods

Get the number of milliseconds remaining until "idle" state detected by timer:

timer.get_remaining_time();

Get the number of milliseconds elapsed on timer since the last "idle" or "active" state transition occurred:

timer.get_elapsed_time();

Get the timestamp in milliseconds of the last detected activity:

timer.get_last_active_time();

Is timer currently indicating an "idle" state?

timer.is_idle();

idle-timer's People

Contributors

spdawson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.