Skip to content

Latest commit

 

History

History
33 lines (22 loc) · 1.03 KB

README.md

File metadata and controls

33 lines (22 loc) · 1.03 KB

While In Use

This small utility (less than 400 bytes minified) allows you to schedule a callback to run on a given interval, but only if activity is detected on the page since the last time the callback ran. The callback will always execute after the first interval, regardless of whether any activity has happened on the page.

Usage

To schedule a callback, pass it and an interval (in milliseconds) to $.whileInUse.

function makeAwesomeSauce() {
  console.log('Sweet!');
}

$.whileInUse(makeAwesomeSauce, 1000);

To discontinue an interval, explicitly return false from the callback.

//Only run this callback 4 times
var count = 0;

function stopAtFour() {
    return ++count != 4;
}

$.whileInUse(stopAtFour, 5000);

Requirements

Requires jQuery - Tested with 2.1.1 but should work with any version that supports .one()

License

Copyright (c) 2014 Rob Widmer. While In Use is released under the MIT License. See the bundled LICENSE file for details.