1

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>

So from above i am getting output as 2699 ( its in seconds = 45min ) and if no event happen its decrements ( 2698..2697..and so on ) and if any event (mouse up..etc ) happen its back to 2699

But i need in minutes thats : 44:59, 44:58 ..and so on

Sagar V
  • 12,158
  • 7
  • 41
  • 68
user3209031
  • 837
  • 1
  • 14
  • 38

3 Answers3

2

Use parseInt((IDLE_TIMEOUT - _idleSecondsCounter)/60) + ":" + (IDLE_TIMEOUT - _idleSecondsCounter)%60; to get achieve hh:mm effect

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>
Sagar V
  • 12,158
  • 7
  • 41
  • 68
1

Do you need this function

  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);
 }

Demo : https://jsfiddle.net/ttqtfwp5/1/

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
1

Here's how I'd code it to be readable

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";
    }
}

uses

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

so that seconds are always two digits

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • Hey Jaromanda , thanks its working ...one more question session timeout event not working on all tab same ...i means say ...if i have open 2 tabs in same browser , so in one tab its 44:06 and on second tab its 44:58 ...if i refresh my first tab ..then it revert to 44:59 but if i see my second tab it showing 43:34....so my question is can i make a popup if timeout is less then 1 min .. saying you will be logout in 1 min and if user click ok he/she will be logout and if he No then its auto reset – user3209031 Aug 21 '17 at 05:29
  • no idea what you are asking here ... are you trying to synchronise the timeout in multiple browser tabs? use localStorage perhaps, or BroadcastChannel in firefox/chrome – Jaromanda X Aug 21 '17 at 05:35
  • yes synchronize same timeout in all tabs ..if i refresh in one tab ...automatically reset all other tab – user3209031 Aug 21 '17 at 05:36
  • 1
    see [this SO question](https://stackoverflow.com/questions/2236828/javascript-communication-between-tabs-windows-with-same-origin) for an answer to that – Jaromanda X Aug 21 '17 at 05:37
  • yes i google and found localstorage .. but need time to build that logic ....hence for timing can i stop user been logout ..as if they have opened many tab ...so might be any one tab is not used and session will be timeout ..hence popup will stop them – user3209031 Aug 21 '17 at 05:39
  • ohh great will look in that....thanks – user3209031 Aug 21 '17 at 05:39
  • i tried : if (oPanel) oPanel.innerHTML = remainMinutes + ':' + remainSeconds; var toald = oPanel.innerHTML; localStorage.setItem("autoupdate",toald); document.getElementById("SecondsUntilExpire").innerHTML = localStorage.getItem("autoupdate"); if (_idleSecondsCounter >= IDLE_TIMEOUT) { //alert("Your Session Time expired. Please Login."); document.location.href = "logoff.php"; } .....but its not working – user3209031 Aug 21 '17 at 07:05
  • well, you'll need more logic than just reading and writing localStorage I'm afraid – Jaromanda X Aug 21 '17 at 09:01
  • ohk ...thanks anyway – user3209031 Aug 21 '17 at 09:05