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

setidle

v0.4.0

Published

Callback for when the system is idle.

Downloads

14

Readme

setIdle()

Monitors your JS application for when the user is idle and triggers events.

var idle = setIdle(() => doSomething('awesome'));

Installation

NPM

> npm install setidle

Bower

> bower install setidle

The old-fashioned way

<script type="text/javascript" src="setidle.js"></script>

Background

Does your JavaScript front-end send back telemetry or do other activities that aren't time sensitive? If so, then a simple way to improve the performance of your application is to queue up those requests until the application is "idle", for example if a user is reading something and not causing the UI to process anything. This can help to ensure that your complex interactions remain smooth, and that you're only running code when you absolutely have the resources to do so, especially on mobile.

[Giphy]

Features

  • Simple no-nonsense browser implementation based on window.setTimeout.
  • Class constructor which wraps event emitters with reasonable duck-typing support.
  • Can be configured to work on a specific HTML element or the entire document.

Examples

CommonJS/Node

var SetIdle = require('setidle');
var idle = new SetIdle(emitter);
idle.start(() => doSomething('awesome'));

RequireJS

require(['setidle'], function (SetIdle) {
    var idle = new SetIdle(emitter);
    idle.start(() => doSomething('awesome'));
});

Browser

var element = document.getElementById('panelStuff');
var idle = new SetIdle(new SetIdle.DOMEventEmitter(element));
idle.start(() => doSomething('awesome'));

Usage

SetIdle (emitter)

  • Constructs an idle monitor.
  • The SetIdle constructor expects a single parameter emitter, which has either an on() or addListener() method, and an off() or removeListener() method.
  • It is compatible with the NodeJS EventEmitter class in the events API.

SetIdle.start (fnIdle, [fnActive], [config])

  • Starts monitoring of the given event emitter.
  • The fnIdle callback will be triggered when the application is idle.
  • The fnActive callback will be triggered when the application is active again.
  • The config object is expected to take the following form. These are defaults if no config is provided:
    {
        /**
         * How long to wait for event silence before declaring the application as idle.
         */
        interval: 3000,

        /**
         * A list of events to use in order to determine when the system is in use.
         */
        events: [

            // The user is interacting with the page.
            'change',
            'click',
            'dblclick',
            'scroll',
            'touchstart',
            'touchend',

            // watch for indications that the browser is having to re-paint the page.
            'resize',
            'mutated' // this is a virtual event, we're using MutationObserver under the hood.
        ]
    }

SetIdle.stop()

  • Stops all monitoring activity.

window.setIdle (fnIdle, [fnActive], [interval])

  • Modeled after window.setTimeout
  • The fnIdle callback will be triggered when the application is idle.
  • The fnActive callback will be triggered when the application is active again.
  • The interval defines how many milliseconds of inactivity constitutes "idleness". The default is 3000 milliseconds.
  • Note that the procedural version automatically passes window.document to the DOMEventEmitter constructor.
  • Returns an instance of the SetIdle object used to start monitoring.

window.clearIdle (idle)

  • Modeled after window.clearTimeout
  • Stops the given SetIdle instance by calling the SetIdle.stop method.

Contributing

Contributions and pull requests are welcome! If you see a feature that you want, add it to our issue tracker. If you find a bug, definitely report back to us.