-
Notifications
You must be signed in to change notification settings - Fork 275
/
testp.js
40 lines (28 loc) · 1.06 KB
/
testp.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
// Get current timestamp
const getCurrentTimestamp = () => {
return new Date().getTime();
};
// Store last active timestamp in LocalStorage
const storeLastActive = () => {
const lastActive = getCurrentTimestamp();
localStorage.setItem('lastActive', lastActive);
};
// Get last active timestamp from LocalStorage
const getLastActive = () => {
return localStorage.getItem('lastActive');
};
// Update last active timestamp on page load and interaction
document.addEventListener('DOMContentLoaded', storeLastActive);
document.addEventListener('click', storeLastActive);
document.addEventListener('scroll', storeLastActive);
document.addEventListener('keydown', storeLastActive);
// Example usage:
const displayLastActive = () => {
const lastActive = getLastActive();
if (lastActive) {
const formattedTime = new Date(parseInt(lastActive)).toLocaleString();
document.getElementById('last-active').innerHTML = `Last active: ${formattedTime}`;
}
};
// Display the last active time on page load
document.addEventListener('DOMContentLoaded', displayLastActive);