Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

280
Visualizações
How reset Timer when i move mouse or click keyboard js?

I making auto logout system with js. How reset timer when I move mouse or click something ? The timer is showing on html. I tried diffrent methods but it still not working.

<div class="nav-link  px-lg-1 " id="ten-countdown"></div>



    countdown( "ten-countdown", 1, 20 );


function countdown( elementName, minutes, seconds )
 {
   var element, endTime, hours, mins, msLeft, time;

function twoDigits( n )
{
    return (n <= 9 ? "0" + n : n);
}

function updateTimer()
{
    msLeft = endTime - (+new Date);
    if ( msLeft < 1000 ) {
        window.location.href = 'index.php';
    } else {
        time = new Date( msLeft );
        hours = time.getUTCHours();
        mins = time.getUTCMinutes();
        element.innerHTML = (hours ? hours + ':' + twoDigits( mins ) : mins) + ':' + twoDigits( time.getUTCSeconds() );
        setTimeout( updateTimer, time.getUTCMilliseconds() + 500 );
    }
}

element = document.getElementById( elementName );
endTime = (+new Date) + 1000 * (60*minutes + seconds) + 500;
updateTimer();

}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

endTime = 0;

countdown("ten-countdown", 1, 20);

function countdown(elementName, minutes, seconds) {
  var element, hours, mins, msLeft, time;

  function twoDigits(n) {
    return (n <= 9 ? "0" + n : n);
  }

  function updateTimer() {
    msLeft = endTime - (+new Date);
    if (msLeft < 1000) {
      window.location.href = 'index.php';
    } else {
      time = new Date(msLeft);
      hours = time.getUTCHours();
      mins = time.getUTCMinutes();
      element.innerHTML = (hours ? hours + ':' + twoDigits(mins) : mins) + ':' + twoDigits(time.getUTCSeconds());
      setTimeout(updateTimer, time.getUTCMilliseconds() + 500);
    }
  }

  element = document.getElementById(elementName);
  endTime = (+new Date) + 1000 * (60 * minutes + seconds) + 500;
  updateTimer();
}

window.addEventListener('mousemove', e => endTime = (+new Date) + 1000 * (60 * 1 + 20) + 500)
<div class="nav-link  px-lg-1 " id="ten-countdown"></div>

The most important thing is the last line. It will reset the endTime variable to 1 minute and 20 seconds in the future whenever the mouse moves.

Notice also that endTime is now a global variable, so that the onMouseMove listener can change it.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda