-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
83 lines (64 loc) · 2 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app"></div>
<script>
const INACTIVE_USER_TIME_THRESHOLD = 2000;
const USER_ACTIVITY_THROTTLER_TIME = 1000;
document.getElementById("app").innerHTML = `
<h1>User is inactive = false</h1>
`;
let userActivityTimeout = null;
let userActivityThrottlerTimeout = null;
let isInactive = false;
activateActivityTracker();
function activateActivityTracker() {
window.addEventListener("mousemove", userActivityThrottler);
window.addEventListener("scroll", userActivityThrottler);
window.addEventListener("keydown", userActivityThrottler);
window.addEventListener("resize", userActivityThrottler);
window.addEventListener("beforeunload", deactivateActivityTracker);
}
function deactivateActivityTracker() {
window.removeEventListener("mousemove", userActivityThrottler);
window.removeEventListener("scroll", userActivityThrottler);
window.removeEventListener("keydown", userActivityThrottler);
window.removeEventListener("resize", userActivityThrottler);
window.removeEventListener("beforeunload", deactivateActivityTracker);
}
function resetUserActivityTimeout() {
clearTimeout(userActivityTimeout);
userActivityTimeout = setTimeout(() => {
userActivityThrottler();
inactiveUserAction();
}, INACTIVE_USER_TIME_THRESHOLD);
}
function userActivityThrottler() {
if (isInactive) {
isInactive = false;
document.getElementById("app").innerHTML = `
<h1>User is inactive = false</h1>
`;
window.parent?.postMessage("experience-activity", "*");
resetUserActivityTimeout();
}
if (!userActivityThrottlerTimeout) {
userActivityThrottlerTimeout = setTimeout(() => {
resetUserActivityTimeout();
clearTimeout(userActivityThrottlerTimeout);
}, USER_ACTIVITY_THROTTLER_TIME);
}
}
function inactiveUserAction() {
isInactive = true;
document.getElementById("app").innerHTML = `
<h1>User is inactive = true</h1>
`;
}
</script>
</body>
</html>