项目作者: ovanderzee

项目描述 :
Do something when a timespan of no interaction passes
高级语言: JavaScript
项目地址: git://github.com/ovanderzee/inactivity-listener.git
创建时间: 2020-12-07T17:19:52Z
项目社区:https://github.com/ovanderzee/inactivity-listener

开源协议:

下载


CircleCI
Coverage Status

Inactivity Listener

Do something when a timespan of no interaction passes.
Straightforward;
It sets a timeout that can be reset by internal eventListeners -
or through the public API.
When time passes your callback is executed.

Features

  • No permission needed unlike in Chrome’s IdleDetector.
  • EventListeners deal with common stopPropagation uses.
  • After timeout, a next timeout with the same parameters can be set.
  • Time passed since last activity available before and after timeout,
    until a (re)start.
  • Event types causing a reset are configurable.
  • Typescript

Install

Install the package as npm package. Provided are
a umd-formatted file in the dist folder to require or just read
and an es-module in the module folder to import.

Usage

Start watching activity, waiting for inactivity.
optional replace standard events to watch:

  1. let interval = 120000 // 20 minutes
  2. let callback = function () {
  3. if (window.alert('Ready to resume?')) {
  4. inactivityListener.restart()
  5. }
  6. }
  7. let events = ['touchstart', 'pointerdown']
  8. inactivityListener.start(interval, callback[, events])

Reset timer when ticking:

  1. inactivityListener.reset()

Report inactivity, in milliseconds:

  1. let inactiveTime = inactivityListener.lapse

Resume listening after timeout:

  1. inactivityListener.restart()

Remove listeners, clear timer:

  1. inactivityListener.stop()

Demo

…/inactivity-listener/demo/inactivityListener.html