Estoy haciendo un sistema de cierre de sesión automático con js. ¿Cómo reinicio el temporizador cuando muevo el mouse o hago clic en algo? El temporizador se muestra en html. Probé diferentes métodos pero todavía no funciona.
<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();}
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> Lo más importante es la última línea. Restablecerá la variable endTime a 1 minuto y 20 segundos en el futuro cada vez que se mueva el mouse.
Tenga en cuenta también que endTime ahora es una variable global, por lo que el oyente onMouseMove puede cambiarla.