会话超时 - 以分钟为单位的结果

时间:2017-08-21 05:01:36

标签: javascript php session-timeout minute



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>
&#13;
&#13;
&#13;

所以从上面我得到的输出是2699(它的秒数= 45分钟)如果没有事件发生它的减量(2698..2697 ......等等)并且如果有任何事件(鼠标向上......等)发生它回到2699

但我需要几分钟时间: 44:59,44:58 ..依此类推

3 个答案:

答案 0 :(得分:2)

使用parseInt((IDLE_TIMEOUT - _idleSecondsCounter)/60) + ":" + (IDLE_TIMEOUT - _idleSecondsCounter)%60;获得 hh:mm 效果

&#13;
&#13;
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";
    }
}
&#13;
<div id='SecondsUntilExpire'></div>
&#13;
&#13;
&#13;

答案 1 :(得分: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/

答案 2 :(得分: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);

所以秒数总是两位数