Know idle mouse time

Asked

Viewed 659 times

4

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

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

The system via ajax, has a counter, so if the page is open, it keeps ordering and recording in the bank that the guy is online.

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

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

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

With this, when spend 30 min without moving the mouse, I can display a dialog on the screen, asking, are you still there? If he doesn’t answer (in up to 30 seconds for example), I’ll drop him.

My doubt would be even, how to know if it makes more than 30 min (for exmeplo) that it does not move the mouse

Any suggestions?

  • See if this helps: http://stackoverflow.com/questions/667555/detecting-idle-time-in-javascript-elegantly

  • Ball show, I’ll test it. Thank you :)

  • Did any of the answers solve your problem?

2 answers

7

Here is 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 each time there is activity of keys, mouse or scroll. In case the counter is not re-started and reaches the end, the function inativo is racing.

0

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

Specifically answering 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);

  • Could reset the seg inside the if

  • I will update the code @Math with a solution closer to reality ^^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.