https://github.com/harunurhan/idlejs
Execute stuff when user is idle or interactive
https://github.com/harunurhan/idlejs
idle interactive no-dependencies typescript
Last synced: 11 months ago
JSON representation
Execute stuff when user is idle or interactive
- Host: GitHub
- URL: https://github.com/harunurhan/idlejs
- Owner: harunurhan
- License: mit
- Created: 2017-12-06T14:50:58.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-18T02:56:58.000Z (about 2 years ago)
- Last Synced: 2024-10-22T14:48:21.384Z (over 1 year ago)
- Topics: idle, interactive, no-dependencies, typescript
- Language: TypeScript
- Homepage:
- Size: 251 KB
- Stars: 37
- Watchers: 4
- Forks: 6
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```bash
yarn add idlejs
npm install --save idlejs
```
### v2 to v3
#### Change imports from `idlejs/dist` to `idlejs`
```js
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
```typescript
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](./src/idle.spec.ts) and [source](./src/idle.ts) 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
```typescript
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](./src/not-idle.spec.ts) and [source](./src/not-idle.ts) code.
### Setting time
Second parameter of `within` is time unit in miliseconds, by default 60000 (a minute).
```typescript
// 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)
```