-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (26 loc) · 840 Bytes
/
index.js
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
class UserInactivityTracker {
constructor(timeout, callback) {
this.timeout = timeout;
this.callback = callback;
this.inactivityTimer = null;
this.events = ['mousemove', 'keydown', 'scroll', 'touchstart'];
this.startTracking();
}
resetTimer() {
clearTimeout(this.inactivityTimer);
this.inactivityTimer = setTimeout(this.callback, this.timeout);
}
startTracking() {
this.events.forEach(event => {
window.addEventListener(event, this.resetTimer.bind(this));
});
this.resetTimer();
}
stopTracking() {
this.events.forEach(event => {
window.removeEventListener(event, this.resetTimer.bind(this));
});
clearTimeout(this.inactivityTimer);
}
}
module.exports = UserInactivityTracker;