所以重點在於檢測用戶是否在某個時間段內在頁面上做了某些事情,如果是,請撥打電話讓其會話保持活動狀態。
我的想法是每當用戶做某事,記錄活動發生的時間。並且定期調用一個檢查函數來確定是否應該進行make-session-alive調用。
首先,請兩個全局變量:
//The time when last make-session-alive call is made.
var lastSyncTime = new Date().getTime();
//The time when user did something on the page. Reassigned per user activity.
var lastActiveTime = lastSyncTime;
二,添加監聽記錄用戶活動時間:
function recordFn(){
lastActiveTime = new Date().getTime();
}
//If there's a lot of clickable stuff, add a click listener to the container
Ext.getBody().on('click', recordFn);
//If there's a lot of input stuff
Ext.getBody().on('keypress', recordFn);
//If ajax call should be detected
Ext.Ajax.on('beforerequest', recordFn);
末,定期調用檢查功能:
SessionExpiredTime = 1000 * 60 * 10; //ten minutes.
function checkFn(){
if(lastActiveTime - lastSyncTime < SessionExpiredTime){
//Make Ajax call to refresh session...
//Reassign
lastSyncTime = new Date().getTime();
lastActiveTime = lastSyncTime;
}
}
setInterval(checkFn, SessionExpiredTime - 1000 * 60);
難道你在這裏省略.getTime(): 'function recordFn(){lastActiveTime = new Date()。getTime();}'? – boatcoder
是的,它不見了。我修改了它。 @ Mark0978 –