Skip to content

Instantly share code, notes, and snippets.

@raininglemons
Created August 24, 2015 19:44
Show Gist options
  • Save raininglemons/ded0def8e0738c460e08 to your computer and use it in GitHub Desktop.
Save raininglemons/ded0def8e0738c460e08 to your computer and use it in GitHub Desktop.
Idler, micro JS lib for binding callbacks to user interactivity and idling.
/**
* Idler
*
* API for binding to user interaction. Created by passing two callbacks, one
* to run on idle and another to run on user interaction resumed. Idler optionally
* takes a third argument, idleTime which defines the period of inactivity required
* to declare a user idled (in ms).
*
* Example usage:
*
* // Instantiates an instance
* //
* var i = new Idler(
* function () {console.warn("Idling...");},
* function () {console.info("Resuming"); },
* 100000 // = 100 seconds
* );
*
*
* // Destroys instance cleanly
* //
* i.destroy();
*
*/
(function () {
"use strict";
var Idler = function (onIdle, onInteraction, idleTime) {
idleTime = idleTime || 60 * 1000;
this.onIdle = onIdle;
this.onInteraction = onInteraction;
this.idleTime = idleTime;
this.isIdle = false;
Idler.initInterval();
Idler.instances.push(this);
};
Idler.getTime = function () {
return +new Date;
};
Idler.instances = [];
Idler.intervalTimer = null;
Idler.lastInteraction = Idler.getTime();
Idler.prototype.destroy = function (instance) {
Idler.instances.splice(Idler.instances.indexOf(this), 1);
if (Idler.instances.length == 0) {
clearInterval(Idler.intervalTimer);
Idler.intervalTimer = null;
}
};
Idler.prototype.setIdle = function () {
this.isIdle = true;
this.onIdle();
};
Idler.setIdle = function () {
Idler.forEach(function (instance) {
instance.setIdle();
});
};
Idler.reset = function () {
Idler.lastInteraction = Idler.getTime();
Idler.forEach(function (instance) {
if (!instance.isIdle)
return;
instance.isIdle = false;
instance.onInteraction();
});
};
Idler.handleTabChange = function (hidden) {
if (hidden)
Idler.setIdle();
else
Idler.reset();
};
Idler.forEach = function (fn) {
for (var i = 0; i < Idler.instances.length; i++)
try {
fn(Idler.instances[i]);
} catch (e) {
console.error(e);
}
};
Idler.update = function () {
var t = Idler.getTime();
Idler.forEach(function (instance) {
if (instance.isIdle)
return;
if (instance.idleTime + Idler.lastInteraction < t)
instance.setIdle();
});
};
Idler.initInterval = function () {
if (Idler.intervalTimer === null)
Idler.intervalTimer = setInterval(Idler.update, 1000);
};
document.addEventListener((function () {
var browserSpecific = {
webkitHidden: "webkitvisibilitychange",
msHidden: "msvisibilitychange",
mozHidden: "mozvisibilitychange"
};
for (var key in browserSpecific) {
if (key in document)
return browserSpecific[key];
}
return "visibilitychange";
})(), function (e) {
var vendor = e.type.replace("visibilitychange", "");
Idler.handleTabChange(document[vendor != "" ? vendor + "Hidden" : "hidden"]);
});
window.onmousemove = Idler.reset;
window.onmousedown = Idler.reset;
window.onclick = Idler.reset;
window.onscroll = Idler.reset;
window.onkeypress = Idler.reset;
window.onkeyup = Idler.reset;
window.Idler = Idler;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment