Skip to content

Instantly share code, notes, and snippets.

@iamandycohen
Created March 1, 2013 14:12
Show Gist options
  • Save iamandycohen/5064903 to your computer and use it in GitHub Desktop.
Save iamandycohen/5064903 to your computer and use it in GitHub Desktop.
JavaScript Inactivity Callback
/// <reference path="jquery-1.8.2.js" />
/// <reference path="Timer.js" />
InactivityPoll = function (inactiveFn, inactiveDelay, confirmStartedFn, confirmFn, confirmDelay, intervalFn, interval, debug) {
/// <param name="inactiveFn" type="Function">Function to call when the inactivityDelay time has been passed</param>
/// <param name="inactiveDelay" type="Number">Amount of time to wait before calling the inactiveFn</param>
/// <param name="confirmStartedFn" type="Function">Function to call when the when the confirmDely starts counting down</param>
/// <param name="confirmFn" type="Function">Function to call when the confirmDelay time has been passed</param>
/// <param name="confirmDelay" type="Number">Amount of time to wait before calling the confirmFn</param>
/// <param name="intervalFn" type="Function">Function to run after the interval specified in the interval parameter</param>
/// <param name="interval" type="Number">Amount of time to run the intervalFn function (frequency)</param>
/// <param name="debug" type="Boolean">Turns debugging on - console messages will display</param>
var _self = this,
_intervalFn = intervalFn,
_interval = interval,
_debug = debug,
_hasConsole = window.console && window.console.log,
_logFn = function (message) {
if (_debug && _hasConsole) {
console.log(message);
}
},
_inactiveFn = inactiveFn,
_inactiveDelay = inactiveDelay,
_confirmStartedFn = confirmStartedFn,
_confirmFn = confirmFn,
_confirmDelay = confirmDelay,
_activity = false,
_inactiveFnCalled = false,
_keepaliveFn = function (e) {
var me = e.data;
//console.log('keepalive');
_activity = true;
},
_listenFn = function () {
$('html')
.off('.inactivitypoll')
.on('mousemove.inactivitypoll keypress.inactivitypoll', this, _keepaliveFn)
};
var _confirmFnWrapper = function () {
_logFn('InactivityPoll - confirmFnWrapper: calling confirmFn');
_confirmFn();
};
var _timers = {
CONFIRM: new Timer(_confirmFnWrapper, _confirmDelay, _intervalFn, _interval)
};
var _inactiveFnWrapper = function () {
_inactiveFnCalled = true;
_logFn('InactivityPoll - inactiveFnWrapper: calling inactiveFn');
_inactiveFn();
_confirmStartedFn();
_timers.CONFIRM.Start();
_logFn('InactivityPoll - inactiveFnWrapper: started confirm timer');
};
var _activityPollFn = function () {
_logFn('InactivityPoll - activityPollFn');
// if there is no activity and _inactiveFnCalled is false then call the _inactiveFnWrapper
if (!_activity & !_inactiveFnCalled) {
_logFn('InactivityPoll - activityPollFn: no activity, inactivityFn not called');
_inactiveFnWrapper();
}
// set the activity to false
_activity = false;
// restart the inactive timer
_timers.INACTIVE.Restart();
_logFn('InactivityPoll - activityPollFn: restarted inactivity timer');
};
_timers.INACTIVE = new Timer(_activityPollFn, _inactiveDelay);
_self.Start = function () {
_logFn('InactivityPoll - Start');
_listenFn();
_timers.INACTIVE.Start();
};
_self.Reset = function () {
_logFn('InactivityPoll - Reset');
_timers.CONFIRM.Stop();
_timers.INACTIVE.Stop();
_activity = false;
_inactiveFnCalled = false;
_self.Start();
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment