Giter VIP home page Giter VIP logo

jquery.idle.js's Introduction

jquery.idle.js

Monitors browser window for activity. Example use cases: polling server less frequently, closing persistent connections, or not running more intensive code while the browser window is inactive.

Usage

Events

Executes callbacks when the window becomes idle or active.

// Register events
$.idle().on('active', function(info) {
  // Do something when the user becomes active
}).on('idle', function(info) {
  // Do something when the user becomes idle
});

// Unregister events (does not support 'active idle' syntax)
var fn = function(info) {};
$.idle().on('active', fn)
        .on('idle', fn);
$.idle().off('active', fn)
        .off('idle', fn);

Info passed to callback

{
  // instance event was registered to
  instance : {},
  // the event type / current state (active|idle)
  type : 'active',
  // time of last state change
  lastTime : 0,
  // the previous state
  lastState : 'idle',
  // milliseconds since last state change
  timeSince : 0
}

Intervals

Similar to setInterval and clearInterval, except it returns the idle object for method chaining and accepts two times: the first is the the interval while the window is active, the second is the interval while the window is idle. Either interval may be omitted or passed a false-y value to disable calling the callback for that state.

// Execute every two seconds while the window is active, sixty seconds
// while the window is idle
$.idle().setInterval(function(info) {
  // Do something
}, 2000, 60000);

// Clears the last interval
$.idle().clearInterval($.idle().lastIntervalID);

// Clears an interval by function
var fn = function(info) {};
$.idle().setInterval(fn, 2000);
$.idle().clearInterval(fn);

Info passed to callback:

{
  // instance callback was registered to
  instance : {},
  // the interval for the current state
  interval : 2000,
  // last time it was executed
  last : 0, 
  // the current state (active|idle)
  state : 'active',
  // milliseconds since last execution
  timeSince : 0 
}

Current state

$.idle().state

Todo

  • Unit tests
  • More extensive cross-browser testing
  • Reference counters for on/off and set/clearInterval to register/unregister events

License

This project is licensed under the MIT license.

jquery.idle.js's People

Contributors

jbboehr avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.