Skip to content

hdodov/active-timeout.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

active-timeout.js

Lets you measure time the user has spent viewing your page. If he focuses another tab or minimizes the browser, that inactive time will not be counted.

Can be seen here

Installation

npm install active-timeout.js

or

git clone https://github.com/hdodov/active-timeout.js

Usage

Make sure to include the library somewhere in your page:

<script type="text/javascript" src="active-timeout.min.js"></script>

API

// Pulse at ~60 intervals per second until the predicate function
// returns a falsy value.
ActiveTimeout.pulse(function (tick) {
    // `tick` holds the time in milliseconds from the last tick to the
    // current one.
    console.log(tick); // ~16
    return true;
});

// Uses `pulse()` and adds its ticks to count time until the predicate
// function returns a falsy value.
ActiveTimeout.count(function (time) {
    // `time` holds the *active* time passed up to this point.
    return true;
});

// Uses `count()` to invoke a callback function after a set amount of
// *active* time has passed.
ActiveTimeout.set(function () {
    // Invoked when the timeout ends.
}, 10000);

// Optionally, `set()` can receive a second function to invoke upon
// each timer tick.
ActiveTimeout.set(function () {
    // Invoked when the timeout ends.
}, function (remainder, tick) {
    // `remainder` holds the time left until the timeout ends
    // `tick`      holds the time since the last tick
}, 10000);

About

JavaScript timeout that counts only while the user is viewing the page.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published