0

By inactivity, I mean no mouse move, no key pressing...

I want to record the duration that users are being inactive. For example, a user is inactive for 1 minute, then re-active, then another 2 minutes of inactivity. I want to have a record like [1, 2].

Both javascript and jquery accepted.

Thanks for any help!

I have a hidden form in html. Thinking to store the record there.The following is what I tried (adopted from another topic). It records every 5 seconds, like 0, 5, 10... If the inactivity lasts for 11 seconds, I want only store 11 instead of 0, 5, 10, which also does not capture 11, but only 10.

var idleTime = 0;
var inactivity = '0'; //record seconds of inactivity
var inactivity_form = $("#id_inacticity");
$(document).ready(function () {
    //Increment the idle time counter every minute.
    var idleInterval = setInterval(timerIncrement, 1000); 

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

    setInterval(inactivity_record, 5000)

});

function timerIncrement() {
    idleTime = idleTime + 1;
    console.log('idletime',idleTime)
}

function inactivity_record() {
    if (idleTime >= 5){
        inactivity = inactivity + ';' + idleTime
        inactivity_form.val(inactivity)
    //console.log('inactivity', inactivity)
    }
}
Jasmin
  • 41
  • 8
  • So what is your problem exactly? – epascarello Jun 22 '20 at 18:28
  • Havent read your code since you included a design description. But read on. Since you want a timer to "reset" when certain things happen, why not just implement it that way? – GetSet Jun 22 '20 at 18:28
  • Oh i see, now reading your code. Javascript has this wonderful piece of code `new Date().getTime()` check it out. That is your timestamp. Reset it when events happen. Your `setInterval()` code should be checking for time *elapsed* from that, provided you save that `getTime()` to a global var or obj property. – GetSet Jun 22 '20 at 18:30
  • i found the solution. – Shkar Sardar Jun 22 '20 at 19:26
  • @Jasmin Does this answer your question ? https://stackoverflow.com/questions/9564602/how-to-know-browser-idle-time/9564811 – Nishant Jun 22 '20 at 19:43

0 Answers0