A library for detecting idle state of browser. Zero dependency, size of 4.7kb. With powerful APIs. -- inspired by idle-timeout
.
- running some tasks while browser idling
- auto pause/resume task(s)
using npm or yarn
npm install idle-state -S
yarn add idle-state -S
using cdn
<script src="https://xxx.xx.xx/idle-state.min.js"></script>
just pass a callback
import idle from 'idle-state'
const foo = () => {
console.log('do task foo.')
}
// just pass a callback, `foo` will be called while browser idling
idle(foo)
pass a task-array
import idle from 'idle-state'
const task1 = () => console.log('do task1.')
const task2 = () => console.log('do task2.')
// tasks-array, `task1` & `task2` will be called while browser idling
idle([task1, task2])
config opitons or both callback & config options
import idle from 'idle-state'
const task1 = () => console.log('do task1.')
const task2 = () => console.log('do task2.')
// with config
idle({
tasks: [task1, task2],
})
// both callback & config options
// in this case task1 & task2 will be converted to an tasks-array [task1, task2]
idle(task1, { tasks: [task2] })
The idle
function has two arguments
import idle from 'idle-state'
idle(callback, options)
callback
- can be a function or function-array[Function]
- considered a single task[Function-array]
- considered a tasks-array
options [Object]
- configuration of how task runningtarget [Element | Element-Array]
- thetarget
would be watched, which determines whether the state is idlingtasks [Function-array]
- tasks-arraytimeout [Number]
- the duration after the browser enters the idle state, at which time the task begins to executeinterval [Number]
- interval of task runingloop [Boolean]
- should task(s) runs loopymousemoveDetect [Boolean]
- should detect mousemove event. Mousemove events are frequently triggered in the browser, so put it configurablereqeustDetect
[Boolean] - should detect requests(ajax or fetch) on browserevents [EventName-array]
- name of events, which would be concated(merged) with default value (scroll
,keydown
,touchmove
,touchstart
,click
)onPause [Function]
- called on tasks pauseonResume [Function]
- called on tasks resumeonDispose [Function]
- called on tasks dispose
const noop = () => {}
const defaultOptions = {
target: document.body,
tasks: [],
timeout: 3000,
interval: 1000,
loop: false,
mousemoveDetect: false,
reqeustDetect: true,
events: ['scroll', 'keydown', 'touchmove', 'touchstart', 'click'],
onPause: noop,
onResume: noop,
onDispose: noop,
}
you can get an instance from idle()
function
import idle from 'idle-state'
const instance = idle(() => {})
instance
.pause(callback)
- pause tasks running.resume(callback)
- resume paused tasks.dispose(callback)
- dispose the resource & remove events handler.push(task-function)
- push a task in current tasks-array.timeout(time)
- setoptions.timeout
the duration after the browser enters the idle state, at which time the task begins to execute (in milliseconds).interval(time)
- setoptions.interval
the tasks running interval (in milliseconds).loop(boolean)
- setoptions.loop
should the tasks runs loopy.isIdle
- get current idle state, return a boolean value
the
callback
passed bymethods
( such aspause(callback)
) has a higher priority thanoptions
( such asoptions.onPause
)
.pause(callback)
instnce.pause()
// with callback
instance.pause(() => console.log('task paused.'))
.resume(callback)
instance.resume()
// with callback
instance.resume(() => console.log('paused task re-running.'))
.dispose(callback)
instance.dispose()
// with callback
instance.dispose(() => console.log('tasks stoped.'))
.push(task)
const task = () => console.log('I am a task.')
// task should be a function
instance.push(task)
.timeout(time)
instance.timeout(2000)
.interval(time)
instance.interval(2000)
.loop(boolean)
instance.loop(true)
import idle from 'idle-state'
const instance = idle(() => {})
// you can get current state by
instance.isIdle // => get a Boolean value
it will get false
while browser trigger event [scroll
, keydown
, touchmove
, touchstart
, click
] by default, you can config options.events
,
which will replace default options.events
npm run dev
npm run build