-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathautorefresh.js
70 lines (58 loc) · 1.66 KB
/
autorefresh.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
var refresh_rate = 5; //<-- In seconds, change to your needs
var last_user_action = 0;
var lost_focus = true;
var focus_margin = 10; // If we lose focus more then the margin we want to refresh
var allow_refresh = true; // on off sort of switch
function keydown(evt) {
if (!evt) evt = event;
if (evt.keyCode == 192) {
// Shift+TAB
toggle_on_off();
}
} // function keydown(evt)
function toggle_on_off() {
if (can_i_refresh) {
allow_refresh = false;
console.log("Auto Refresh Off");
} else {
allow_refresh = true;
console.log("Auto Refresh On");
}
}
function reset() {
last_user_action = 0;
console.log("Reset");
}
function windowHasFocus() {
lost_focus = false;
console.log(" <~ Has Focus");
}
function windowLostFocus() {
lost_focus = true;
console.log(" <~ Lost Focus");
}
setInterval(function () {
last_user_action++;
refreshCheck();
}, 1000);
function can_i_refresh() {
if (last_user_action >= refresh_rate && lost_focus && allow_refresh) {
return true;
}
return false;
}
function refreshCheck() {
var focus = window.onfocus;
if (can_i_refresh()) {
window.location.reload(); // If this is called no reset is needed
reset(); // We want to reset just to make sure the location reload is not called.
} else {
console.log("Timer");
}
}
window.addEventListener("focus", windowHasFocus, false);
window.addEventListener("blur", windowLostFocus, false);
window.addEventListener("click", reset, false);
window.addEventListener("mousemove", reset, false);
window.addEventListener("keypress", reset, false);
window.onkeyup = keydown;