-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_timer.js
39 lines (33 loc) · 1.08 KB
/
session_timer.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
//設定閒置時間 單位ms
const idle = 900000;
let timeout;
//重置計時器的函數
function resetTimer() {
//先重製上一次的timout
clearTimeout(timeout);
//到達閒置時間,要求重新登入並刪除session、cookie
timeout = setTimeout(() => {
fetch('session_timeout.php')
.then(response => response.json())
.then(data => {
if (!data.success) {
alert(data.message);
window.location.href = 'login.html';
}
})
.catch(error => console.error('Error:', error));
}, idle);
//重新設置 session 和 cookie 過期時間
fetch('reset_session_cookie.php')
.then(response => response.json())
.then(data => {
if (!data.success) {
console.error('Failed to reset session and cookie');
}
})
.catch(error => console.error('Error:', error));
}
//監聽鼠標、鍵盤、刷新頁面事件
document.addEventListener('mousemove', resetTimer);
document.addEventListener('keypress', resetTimer);
window.onload = resetTimer;