npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

idle-state

v0.3.0

Published

Running some tasks while browser idling

Downloads

4

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 -S
yarn add idle-state -S

using 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 running
    • target [Element | Element-Array] - the target would be watched, which determines whether the state is idling
    • tasks [Function-array] - tasks-array
    • timeout [Number] - the duration after the browser enters the idle state, at which time the task begins to execute
    • interval [Number] - interval of task runing
    • loop [Boolean] - should task(s) runs loopy
    • mousemoveDetect [Boolean] - should detect mousemove event. Mousemove events are frequently triggered in the browser, so put it configurable
    • reqeustDetect [Boolean] - should detect requests(ajax or fetch) on browser
    • events [EventName-array] - name of events, which would be concated(merged) with default value (scroll, keydown, touchmove, touchstart, click)
    • onPause [Function] - called on tasks pause
    • onResume [Function] - called on tasks resume
    • onDispose [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) - set options.timeout the duration after the browser enters the idle state, at which time the task begins to execute (in milliseconds)
  • .interval(time) - set options.interval the tasks running interval (in milliseconds)
  • .loop(boolean) - set options.loop should the tasks runs loopy
  • .isIdle - get current idle state, return a boolean value

the callback passed by methods ( such as pause(callback) ) has a higher priority than options ( such as options.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 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

demo

npm run dev

build

npm run build