如果你想要X時間後關閉會話,無論正在做Ajax請求,但前提是用戶在網頁上沒有任何活動,你可以使用此代碼,我使用的是:
(function() {
// After 30 minutes without moving the mouse, the user will be redirect to logout page
var time2refresh = 30;
// This is how many time the user has to be inactive to trigger the countdown of 30 minutes
var timeInactive = .5;
// This will store the timer in order to reset if the user starts to have activity in the page
var timer = null;
// This will store the timer to count the time the user has been inactive before trigger the other timer
var timerInactive = null;
// We start the first timer.
setTimer();
// Using jQuery mousemove method
$(document).mousemove(function() {
// When the user moves his mouse, then we stop both timers
clearTimeout(timer);
clearTimeout(timerInactive);
// And start again the timer that will trigger later the redirect to logout
timerInactive = setTimeout(function() {
setTimer();
}, timeInactive * 60 * 1000);
});
// This is the second timer, the one that will redirect the user if it has been inactive for 30 minutes
function setTimer() {
timer = setTimeout(function() {
window.location = "/url/to/logout.php";
}, time2refresh * 60 * 1000);
}
})();
所以這個功能的邏輯是這樣的:
1)用戶登錄到您的網站 2)。5分鐘(30秒後)O如果用戶移動鼠標,兩個定時器都將被重置,而第一個定時器將重新開始。 4)如果在30分鐘後用戶不移動他的鼠標,那麼它將被重定向到註銷頁面,關閉他的會話。
當然,你已經回答了你自己的問題。如果問題只出現在帶有Ajax的頁面上,並且每10秒發送一次請求,那麼它確實會重置超時。您可以通過手動跟蹤會話超時來解決此問題。 – Aneri
根據您對情況的描述,您對問題的評估看起來很準確。你有問題可以幫助你解決嗎? –
感謝您的幫助。我怎樣才能跟蹤會話,或者如何在閒置30分鐘後終止會話? –