2017-08-21 123 views
0

會話超時 - 結果分鐘

var IDLE_TIMEOUT = 2700; //seconds 45min 
 

 
var _idleSecondsCounter = 0; 
 
document.onclick = function() { 
 
    _idleSecondsCounter = 0; 
 
}; 
 
document.onmousemove = function() { 
 
    _idleSecondsCounter = 0; 
 
}; 
 

 
document.onkeypress = function() { 
 
    _idleSecondsCounter = 0; 
 
}; 
 
window.setInterval(CheckIdleTime, 1000); 
 

 
function CheckIdleTime() { 
 
    _idleSecondsCounter++; 
 
    var oPanel = document.getElementById("SecondsUntilExpire"); 
 
    if (oPanel) 
 
    oPanel.innerHTML = (IDLE_TIMEOUT - _idleSecondsCounter) + ""; 
 
    if (_idleSecondsCounter >= IDLE_TIMEOUT) { 
 
    //alert("Your Session Time expired. Please Login."); 
 
    document.location.href = "logoff.php"; 
 
    } 
 
}
<div id='SecondsUntilExpire'></div>

所以從上面我得到的輸出爲2699(其以秒= 45分鐘),如果沒有事件發生的遞減(2698..2697..and所以上),如果任何事件(鼠標up..etc)發生了回2699

但我需要在幾分鐘內那:44:59,44:58 ..和等

+1

退房這種情況下,您需要格式化https://stackoverflow.com/questions/3733227/javascript-seconds-to-minutes- and-seconds – Scuzzy

+0

你使用什麼瀏覽器? –

+0

我正在使用火狐 – user3209031

回答

1

以下是我想編寫代碼的時候是可讀

function CheckIdleTime() { 
    _idleSecondsCounter++; 
    var oPanel = document.getElementById("SecondsUntilExpire"); 
    var remain = IDLE_TIMEOUT - _idleSecondsCounter; 
    var remainMinutes = Math.floor(remain/60); 
    var remainSeconds = ('0' + (remain % 60)).substr(-2); 
    if (oPanel) 
     oPanel.innerHTML = remainMinutes + ':' + remainSeconds; 
    if (_idleSecondsCounter >= IDLE_TIMEOUT) { 
     //alert("Your Session Time expired. Please Login."); 
     document.location.href = "logoff.php"; 
    } 
} 

使用

var remainSeconds = ('0' + (remain % 60)).substr(-2); 

使秒總是兩位數

+0

嘿Jaromanda,感謝它的工作...一個問題會話超時事件不工作在所有標籤相同...我的意思是說...如果我已經在同一瀏覽器中打開2個選項卡,所以在一個標籤44:06,第二個標籤44:58 ...如果我刷新我的第一個標籤..然後恢復到44:59,但如果我看到我的第二個標籤顯示43:34 ... .so我的問題是,如果超時時間少於1分鐘,我可以彈出一個彈出窗口..說你將在1分鐘內註銷,如果用戶點擊確定,他/她將註銷,如果他沒有,那麼它的自動重置 – user3209031

+0

不知道什麼你問這裏...你是否試圖在多個瀏覽器標籤中同步超時?使用localStorage也許,或者在Firefox/Chrome中的BroadcastChannel –

+0

是同步所有標籤中的同一超時..如果我在一個標籤刷新...自動重置所有其他標籤 – user3209031

2

使用parseInt((IDLE_TIMEOUT - _idleSecondsCounter)/60) + ":" + (IDLE_TIMEOUT - _idleSecondsCounter)%60;到達到個HH:MM效果

var IDLE_TIMEOUT = 2700; //seconds 45min 
 

 
var _idleSecondsCounter = 0; 
 
document.onclick = function() { 
 
    _idleSecondsCounter = 0; 
 
}; 
 
document.onmousemove = function() { 
 
    _idleSecondsCounter = 0; 
 
}; 
 

 
document.onkeypress = function() { 
 
    _idleSecondsCounter = 0; 
 
}; 
 
window.setInterval(CheckIdleTime, 1000); 
 

 
function CheckIdleTime() { 
 
    _idleSecondsCounter++; 
 
    var oPanel = document.getElementById("SecondsUntilExpire"); 
 
    if (oPanel) 
 
     oPanel.innerHTML = parseInt((IDLE_TIMEOUT - _idleSecondsCounter)/60) + ":"; 
 
     oPanel.innerHTML += (IDLE_TIMEOUT - _idleSecondsCounter)%60<10?"0"+(IDLE_TIMEOUT - _idleSecondsCounter)%60:(IDLE_TIMEOUT - _idleSecondsCounter)%60; 
 
    if (_idleSecondsCounter >= IDLE_TIMEOUT) { 
 
     alert("Your Session Time expired. Please Login."); 
 
     //document.location.href = "logoff.php"; 
 
    } 
 
}
<div id='SecondsUntilExpire'></div>

+0

@JaromandaX感謝和代碼更新。現在它會顯示'1:01' –

1

您是否需要此功能

var IDLE_TIMEOUT = 2700; 
    alert(getMinutes(IDLE_TIMEOUT)); 
    function getMinutes(time){ 
     minutes = time/60; 
     seconds = time%60; 
     return ("00" + minutes).substr(-2)+":"+("00" + seconds).substr(-2); 
} 

演示:https://jsfiddle.net/ttqtfwp5/1/