idle-state
v0.3.0
Published
Running some tasks while browser idling
Readme
introduce
A library for detecting idle state of browser. None dependency, size of 4.7kb. With powerful APIs. -- inspired by idle-timeout.
what I can do
Running some tasks while browser idling
install
using npm or yarn
npm install idle-state -Syarn add idle-state -Susing cdn
<script src="https://xxx.xx.xx/idle-state.min.js"></script>usage
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] })document
The idle function has two arguments
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]- thetargetwould 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
options default value
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,
}methods
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.timeoutthe duration after the browser enters the idle state, at which time the task begins to execute (in milliseconds).interval(time)- setoptions.intervalthe tasks running interval (in milliseconds).loop(boolean)- setoptions.loopshould the tasks runs loopy.isIdle- get current idle state, return a boolean value
the
callbackpassed 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)get current idle state
import idle from 'idle-state'
const instance = idle(() => {})
// you can get current state by
instance.isIdle // => get a Boolean valueit 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
demo
npm run devbuild
npm run build