Know idle mouse time

4

I have an intranet system, which sellers attend some classes.

Sometimes they leave the window open at that URL for days.

The system via ajax has a counter, so if the page is open, it will be requesting and recording on the bank that the face is online.

But as I said, he can leave the window open for days.

I think the best way, would be to catch if he moved the mouse.

So what I need is, how to know if it takes more than 30 minutes (for example) that it does not move the mouse.

With this, when I spend 30 minutes without moving the mouse, I can display a dialogue on the screen, asking, are you still there? if it does not respond (within 30 seconds for example), I uncheck it.

My question would be, how do I know if it does more than 30 minutes (for example) that it does not move the mouse

Any suggestions?

    
asked by anonymous 05.12.2016 / 18:56

2 answers

7

Here's a suggestion:

var tempoDeEspera = 30 * 60 * 1000;
var timeout = setTimeout(inativo, tempoDeEspera);

function actividade(e) {
  clearInterval(timeout);
  timeout = setTimeout(inativo, tempoDeEspera);
  // só para o exemplo
  console.log('Houve actividade de ' + (e.type == 'keyup' ? 'teclado' : 'ponteiro'));
}

function inativo() {
  console.log('Inativo hà mais de 30 minutos...');
}

['keyup', 'touchmove' in window ? 'touchmove' : 'mousemove', "onwheel" in document.createElement("div") ? "wheel" : document.onmousewheel !== undefined ? "mousewheel" : "DOMMouseScroll"].forEach(function(ev) {
  window.addEventListener(ev, actividade);
});

The idea is to delete the current counter and start a new one every time there is a key, mouse or scroll activity. In case the counter is not re-started and reaches the end, the inativo function is run.

    
05.12.2016 / 19:07
0
  

I think the best way, would be to catch if he moved the mouse.

Responding specifically to the above question, you can identify with the following javascript code:

//variável representando o tempo inativo atual
seg = 0;
//Adicionando ao document o evento a ser disparado sempre que o mouse se mover
document.addEventListener("mousemove", function(){  
  //Caso seja detectado o movimento do mouse, atualizar a variável que representa o tempo de inatividade para 0 segundos
  seg = 0;
});
//A cada 1 segundo (a seu critério), adicionar +1 segundo de tempo de inatividade à variável "seg". Caso tenha chegado a 10 segundos, exibirá um alert.
setInterval(function(){ 
  seg = seg + 1; 
  if(seg == 10)
    alert("Dez segundos inativo.");
}, 1000);
    
05.12.2016 / 19:14