-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidleTimer.js
More file actions
88 lines (63 loc) · 2.9 KB
/
idleTimer.js
File metadata and controls
88 lines (63 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// idleTimer
// instantiable class, instance of which can be configured to take an action on a certain-amount-of-time-exceeded.
// instances accept an options object on instantiation for configuration.
// available OPTIONS:
// interval: amount of time in milliseconds in between check for idleness; default = 1 min.
// idleTimeAlllowded: amount of time in milliseconds that can pass before someone is deemed idle; default = 20 min.
// onTimeExceeded: callback run when allowed idle-time is exceeded; default = console.log message.
// onNewActivity: callback run after a user has been idle and new activity is detected; default = console.log message.
//
// Dependencies:
// underscore/lodash's bindAll Method
// jQuery
(function() {
idleTimer = function(options) {
var idleTimerInstance = {
initialize: function() {
_.bindAll(idleTimerInstance, 'initialize', 'noteActivity', 'incrementTimer', 'start');
options = options || {};
this.timeSpentIdle = 0; //in milliseconds
this.state = "tracking_idleness";
//in milliseconds; default is 1 min:
this.interval = options.interval || 60*1000;
//in milliseconds; default is 20 min:
this.maxIdleTime = options.maxIdleTime || 20*60*1000;
this.onTimeExceeded = options.onTimeExceeded || function() {
console.log( "idleTimer: You have been idle for more than " +
(Math.floor((this.maxIdleTime / 1000 / 60)))+" minutes" )
};
this.onNewActivity = options.onNewActivity || function() {
console.log( "idleTimer: New Activity Recorded" );
};
_.bindAll(idleTimerInstance, "onTimeExceeded", "onNewActivity")
},
incrementTimer: function() {
this.timeSpentIdle = this.timeSpentIdle + this.interval;
console.log("incrementTimer (interval, timeSpentIdle, maxIdleTime): ", this.interval, this.timeSpentIdle, this.maxIdleTime );
// If user has exceeded max amount of time, then call fn
if (this.timeSpentIdle >= this.maxIdleTime) {
this.timeSpentIdle = 0;
this.state = "watching_for_new_activity";
this.onTimeExceeded();
clearInterval(this.intervalId);
};
},
noteActivity: function(e) {
this.timeSpentIdle = 0;
if (this.state == "watching_for_new_activity") {
this.state = "tracking_idleness";
this.intervalId = setInterval(this.incrementTimer, this.interval);
this.onNewActivity();
}
},
start: function () {
$(document).mousemove(this.noteActivity);
$(document).keypress(this.noteActivity);
this.intervalId = setInterval(this.incrementTimer, this.interval);
}
};
idleTimerInstance.initialize();
return idleTimerInstance;
}; //end idleTimerInstance
return idleTimer;
}) ();