forked from henriqueboaventura/jquery.idle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanilla.idle.js
114 lines (105 loc) · 3.5 KB
/
vanilla.idle.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* @file: vanilla.idle.js
* @title: Vanilla Idle.
* @description: A simple VanillaJS port of this jQuery plugin.
* @author @hugohil
* @version 1.2.7
* @license https://opensource.org/licenses/MIT
*/
(function (){
'use strict';
window.idle = function (options){
var defaults = {
idle: 60000, // idle time in ms
events: ['mousemove', 'keydown', 'mousedown', 'touchstart'], // events that will trigger the idle resetter
onIdle: function (){}, // callback function to be executed after idle time
onActive: function (){}, // callback function to be executed after back form idleness
onHide: function (){}, // callback function to be executed when window become hidden
onShow: function (){}, // callback function to be executed when window become visible
keepTracking: true, // set it to false of you want to track only once
startAtIdle: false, // set it to true if you want to start in the idle state
recurIdleCall: false
};
var settings = extend({}, defaults, options);
var idle = settings.startAtIdle;
var visible = !settings.startAtIdle;
var visibilityEvents = ['visibilitychange', 'webkitvisibilitychange', 'mozvisibilitychange', 'msvisibilitychange'];
var lastId = null;
var resetTimeout, timeout;
// event to clear all idle events
window.addEventListener('idle:stop', function (event){
bulkRemoveEventListener(window, settings.events);
settings.keepTracking = false;
resetTimeout(lastId, settings);
});
var resetTimeout = function resetTimeout (id, settings){
if(idle){
idle = false;
settings.onActive.call();
}
clearTimeout(id);
if(settings.keepTracking){
return timeout(settings);
}
};
var timeout = function timeout (settings){
var timer = (settings.recurIdleCall) ? setInterval : setTimeout;
var id;
id = timer(function (){
idle = true;
settings.onIdle.call();
}, settings.idle);
return id;
};
return {
start: function (){
lastId = timeout(settings);
bulkAddEventListener(window, settings.events, function (event){
lastId = resetTimeout(lastId, settings);
});
if(settings.onShow || settings.onHide){
bulkAddEventListener(document, visibilityEvents, function (event){
if (document.hidden || document.webkitHidden || document.mozHidden || document.msHidden) {
if (visible){
visible = false;
settings.onHide.call();
}
} else {
if (!visible){
visible = true;
settings.onShow.call();
}
}
});
}
}
}
};
var bulkAddEventListener = function bulkAddEventListener (object, events, callback){
events.forEach(function (event){
object.addEventListener(event, function (event){
callback(event);
});
});
};
var bulkRemoveEventListener = function bulkRemoveEventListener (object, events){
events.forEach(function (event){
object.removeEventListener(event);
});
};
// Thanks to http://youmightnotneedjquery.com/
var extend = function extend (out){
out = out || {};
for (var i = 1; i < arguments.length; i++) {
if (!arguments[i]){
continue;
}
for (var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)){
out[key] = arguments[i][key];
}
}
}
return out;
};
})();