jQuery Plugin: Activity
The activity plugin takes care of all the underlying work in tracking user's (in)activity with the smallest amount of interference as possible. The plugin utilizes jQuery's one method on specified intervals to track key strokes and mouse movement in relation to the current window.
UsageTo start tracking user's activity, call the init method described later in this page. Simplicity is key with this hundred line script, with only four possible options to manipulate.
- inactive: Time in milliseconds of user inactivity allowed (defaults to 30 mins)
- inactiveFn: Callback function when above inactive time is reached
- interval: Time in milliseconds to check user activity in intervals (defaults to 5 mins)
- intervalFn: Callback function when above interval time is reached
The activity object comes with a few methods that may be of use. Remember that the init function needs to be called before the plugin will start tracking the users activity.
- init: Initializes the activity tracker. Takes object of settings as it's single parameter.
- update: Updates the users last active time to the current time stamp, or one that is provided.
- isActive: Returns a boolean value of the user's current active state.
- getActivity: Returns an object containing the users activity information.
- reActivate: Re-activates the activity plugin after a user has gone inactive.
- now: Returns the current time stamp.
jQuery(function($){
// Initialize the activity check
$.activity.init({
// Set interval check to every 5 seconds
interval: 1000*5,
intervalFn: function(info){
console.log('Interval Check - Last Active:', info.lastActive, ', Difference in milliseconds to current time:', info.diff);
},
// Set inactive check to every 15 seconds
inactive: 1000*5*3,
inactiveFn: function(info){
console.warn('Inactive Triggered - Last Active:', info.lastActive, ', Difference in milliseconds to current time:', info.diff);
}
});
// Either reactivate, or update the current timestamp when user clicks on the page
$(document).click(function(){
if ( $.activity.isActive() )
$.activity.update();
else
$.activity.reActivate();
});
});
If you pop open firebug, There will be log statements indicating either intervals or inactivity on the current window (depending on how long it took to get to this point). If there is a warning log, this means that the inactive flag was triggered, and the plugin has stopped tracking. To reactivate the tracker, simply click on the screen.
As always the source code is provided below, along with a zip file containing an example page to test on locally.
/*!
* jQuery Activity
* December 20, 2009
* Corey Hart @ http://www.codenothing.com
*/
(function($, undefined){
// Current Timestamp
function now(){
return (new Date).getTime();
}
// Attach activity object to jQuery base object
$.activity = {
// Variable Storage
defaults: {
// Users last active timestamp
lastActive: now(),
// Interval times in milliseconds
inactive: 1000*60*30, // Default inactive at 30 minutes
interval: 1000*60*5, // Default interval check every 5 minutes
// Callback functions when intervals are reached
inactiveFn: function(){},
intervalFn: function(){}
},
// Timerid
timer: undefined,
// Expose the current time fn
now: now,
// Starts the interval
init: function(options){
// Update vars with options
var self = this;
self.defaults = $.extend(self.defaults, options||{});
self.defaults.lastActive = now();
// Check activity on a continuous interval
self.bind();
return self.timer = setInterval(function(){ self.check(); }, self.defaults.interval);
},
// Updates lastActive to current/provided timestamp
update: function(time){
return (this.defaults.lastActive = time === undefined ? now() : time);
},
// Returns boolean indicator of activity
isActive: function(){
return (this.timer !== undefined);
},
// Getter for current activity
getActivity: function(){
var self = this, time = now();
return {diff: time-self.defaults.lastActive, time: time, lastActive: self.defaults.lastActive};
},
// Re-activates the interval (options only required if change to defaults wanted)
reActivate: function(options){
var self = this;
// Clear interval if still running
if (self.timer)
self.timer = clearInterval(self.timer);
// Reactivation
return self.init(options||{});
},
// Interval check function
check: function(){
var self = this,
time = now(),
diff = time - self.defaults.lastActive;
// Trigger appropriate callback
if (diff > self.defaults.inactive){
self.defaults.inactiveFn.call(self, {diff: diff, time: time, lastActive: self.defaults.lastActive});
return self.timer = clearInterval(self.timer);
}
else if (diff > self.defaults.interval){
self.defaults.intervalFn.call(self, {diff: diff, time: time, lastActive: self.defaults.lastActive});
}
// Rebind mouse tracking
return self.bind();
},
// Rebinds activity events
bind: function(){
var self = this;
$(window).unbind('.activity').one('mousemove.activity keyup.activity', function(event){
self.defaults.lastActive = event.timeStamp;
});
return self;
}
};
})(jQuery);
Download
Latest: activity-plugin.zipReleased: December 20, 2009
RSS