Git Product home page Git Product logo

idlejs's Introduction

idlejs

Execute a function only when certain events on certain target element have or have not occured within given timeout.

It's simple, configurable, typescript friendly and has an easy chainable API.

Install

yarn add idlejs

npm install --save idlejs

v2 to v3

Change imports from idlejs/dist to idlejs

import { ... } from 'idjejs' 

Idle

Excutes the callback function (do) when none of the specified events have occurred within given time, in other words when user is idle.

Usage

import { Idle } from 'idlejs';

// with predefined events on `document`
const idle = new Idle()
  .whenNotInteractive()
  .within(5)
  .do(() => logoutUser())
  .start();

// another example with custom events which are useful if events aren't bubbling up to the document
const idle = new Idle()
  .whenNot([{
    events: ['click', 'hover'],
    target: buttonEl,
  },
  {
    events: ['click', 'input'],
    target: inputEl,
  },
  ])
  .whenNotInteractive()
  .within(10)
  .do(logoutUser)
  .start();

For more features or examples please check the tests and source code.

NotIdle

Executes the callback function (do), if at least one of the specified events have occured within given time, in other words when user is not idle or interactive.

Usage

import { NotIdle } from 'idlejs';

// with predefined events on `document`
const idle = new Idle()
  .whenInteractive()
  .within(10)
  .do(() => log('user was active in the last 10 minutes'))
  .start();

// another example with custom events which are useful if events aren't bubbling up to the `document`
const notIdle = new NotIdle()
  .when([{
    events: ['click', 'hover'],
    target: buttonEl,
  },
  {
    events: ['click', 'input'],
    target: inputEl,
  },
  ])
  .whenInteractive()
  .within(10)
  .do(() => log('user was active in the last 10 minutes'))
  .start();

For more features or examples please check the tests and source code.

Setting time

Second parameter of within is time unit in miliseconds, by default 60000 (a minute).

// will trigger if nothing happens for 5 minutes
new Idle()
  .within(5)

// will trigger if nothing happens for 5 seconds
new Idle()
  .within(5, 1000)

idlejs's People

Contributors

harunurhan avatar msftenhanceprovenance avatar pamfilos avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

idlejs's Issues

idlejs does not work with Jest (https://jestjs.io/)

Our Angular application uses Jest for unit testing, and we found out that idlejs library does not work with Jest. See errors below:

● Test suite failed to run

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
 • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
 • If you need a custom transformation specify a "transform" option in your config.
 • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

C:\dev\src\trunk\my-apps\node_modules\idlejs\dist\index.js:1
export { Idle } from './idle';
^^^^^^

SyntaxError: Unexpected token 'export'

>  8 | import {Idle, NotIdle} from 'idlejs/dist';
     | ^
   9 | 
  10 | @Injectable({
  11 |   providedIn: 'root'

  at Runtime._execModule (../../node_modules/jest-runtime/build/index.js:1179:56)

Working Fine in Desktop , but not in Mobile Devices

i tried to check the idle timer in my webapp , it is working in Desktop but when i try to access the same in the mobile devices , the idle timer is not working .
Steps:
Log in to my web App in my mobile (Chrome Browser)
Push it to Background app
Lock my mobile device
Come back after 15 mins to check , if i see a modal about User Inactive.
Result: I could not see any dialog box. Is the timer is working ?.. Can u please help me

Here is my Code To check the Idle Timer :
const TouchDeviceIdle = new Idle()
.whenNot([
{
events: ['touchcancel','touchmove','touchstart','focus','focusin'],
target: document,
},
])
.whenNotInteractive()
.within(15)
.do(() => {
if(this.sessionModalShow){
TouchDeviceIdle.stop();
}else{
this.userInactive(TouchDeviceIdle);
}
})
.start(); */

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.