Souheil Diab
Souheil Diab

Reputation: 41

counter in seconds idle jquery

i have this function in Idle library, but what i need is to calculate the action time in second, i mean the active time(onclick, onscroll and keypress).

function is:

    (function () { 
var minutes = false;
var interval = 1000; 
var IDLE_TIMEOUT = 5; 
var idleCounter = 0;
var counter=0;

document.onclick = document.onkeypress = function () {
    idleCounter = 0;
    setInterval(function () {
++counter;; 
 }, 1000);

};

window.setInterval(function () {
    if (++idleCounter >= IDLE_TIMEOUT) {
    alert(counter);
        document.location.href = "SessionExpired.aspx";
    }
}, interval);
}());

this function will wait for 5 seconds, if no action on the page, so i will be redirected to SessionExpired.aspx. if there is action, so am doing ++couter each second.

I need when this counter in seconds.

Thank you.

Upvotes: 1

Views: 1262

Answers (3)

Souheil Diab
Souheil Diab

Reputation: 41

This is what i wanted exactly and i did it:

    <script  src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" ></script>
      <script src="./e-lawyer/JS/idle.js"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>timertest</title>
    <script language="javascript">
    var it;
    x = 0;
    $(document).ready(function(){
        $('.status').html('active');
    });


    function count() { 
    x+=1;
    $('.usercount').html(x);
    }

    (function() {

var timeout = 2000;

$(document).bind("idle.idleTimer", function() {
clearInterval(it);    
    $('.status').html('idle');
});


$(document).bind("active.idleTimer", function() {
  it = setInterval(count, 1000);
 $('.status').html('active');
});
       $.idleTimer(timeout);

    })(jQuery);

    </script>
    </head>

    <body>
    <div class="status" style="border:1px dashed black; width:500px; height:50px;"></div>
    <div class="usercount"style="border:1px dashed black; width:500px; height:50px;"></div>
    </body>
    </html>

Upvotes: 0

Datsik
Datsik

Reputation: 14824

You can just reset the timer

var counter;
var counterSeconds;

document.onclick = document.onkeypress = function () {
    idleCounter = 0; // Set counter to 0 on each keypress/page click
    clearInterval(counter) // Every time they click, clear the old interval and start a new one below
    counter = setInterval(function () { // assign the interval to a variable so we can clear it
       if (idleCounter > IDLE_TIMEOUT) { // Check if they've idled too long
            document.location.href = "SessionExpired.aspx"; // Redirect them if they have
       } 
       ++counterSeconds;
       ++idleCounter; // Should increment 1/sec depending on your computer.
    }, 1000); // Ticks at 1000 milliseconds (1 Second)
};

Upvotes: 1

Zaheer Ahmed
Zaheer Ahmed

Reputation: 28528

One problem here is that you start new interval function for each click or keypress event which causes multiple threads to update same variable.

You should start an interval thread outside the event.

try this:

document.onclick = document.onkeypress = function () {
    idleCounter = 0;
};

var activeTimer = setInterval(function () {
   ++counter; 
}, interval);

var idleTimer = window.setInterval(function () {
    if (++idleCounter >= IDLE_TIMEOUT) {
        alert(counter);
        document.location.href = "SessionExpired.aspx";
    }
}, interval);

Upvotes: 0

Related Questions