0

I use countdown timer to destroy my session if no action is being made.

  var idleTime = 0;
  function Timer(duration, display)  {
    var timer = duration, hours, minutes, seconds;
    setInterval(function () {
        hours = parseInt((timer /3600)%24, 10)
        minutes = parseInt((timer / 60)%60, 10)
        seconds = parseInt(timer % 60, 10);

        hours = hours < 10 ? "0" + hours : hours;
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;

        display.text(hours +":"+minutes + ":" + seconds);
        if(hours == 0 && minutes == 0 && seconds == 0){
            $.ajax({
              url:'../ajax/logoutuser.php?action=logout',

              success: function(data){
                window.location.href = '../index.php';
              }
            });
        }else{
           --timer;
        }

    }, 1000);
}

function timerIncrement() {
    idleTime = idleTime + 1;
    if (idleTime > 1) { // 20 minutes
         var twentyFourHours = 1 * 60 * 60;
        var display = $('#time');
        Timer(twentyFourHours, display);
    }
}

$(document).ready(function(){
   var idleInterval = setInterval(timerIncrement, 60000); // 1 minute

    //Zero the idle timer on mouse movement.
    $(this).mousemove(function (e) {
        idleTime = 0;
    });
    $(this).keypress(function (e) {
        idleTime = 0;
    });

});

It works at first but the problem is when the countdown timer starts to runs even if I make any action from my page like mouse click/mouse move/ key press it doesn't refresh the count down timer.. what I wanted was if once that the countdown timer runs if I make another action count down timer will reload.

Like when I refresh my page the countdown timer also refresh.. is there a way to possible do it?.

Felix
  • 5,452
  • 12
  • 68
  • 163
Lion Smith
  • 647
  • 3
  • 16
  • 48

1 Answers1

0

If you want to reset you setInterval, SO has already an answer: https://stackoverflow.com/a/8126515/6053654

P.S.
  • 15,970
  • 14
  • 62
  • 86