Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
Unable to reset setTimeout timer in JavaScript

Here is my pseudocode:

if(key.pressed = 'a') {
  activate = true;
}

var mytimeout = function timer_function() {
   // Do stuff;
   setTimeout( function() {
    // do more stuff
   }, 5000);
}

function some_function() {
  
 if(activated) {
   // do stuff
   clearTimeout(mytimeout);

   timer_function();
 }
}

if(mouse.click == true) {
  some_function();
}

I want the timer to be reset on each mouse click which calls some_function(). What actually happens is that the time is set on first click but never resets after that.

What am I missing?

Thanks.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

mytimeout is a function, not a timeout handle. What you need to store is the result of the setTimeout call like this.

var timeoutHandle;
function timer_function() {
   // Do stuff;
   timeoutHandle = setTimeout( function() {
    // do more stuff
   }, 5000);
}

function some_function() {
  
 if(activated) {
   // do stuff
   clearTimeout(timeoutHandle);

   timer_function();
 }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

In function timer_function return the value of setTimeout

var mytimeout = function timer_function() {
   return setTimeout( function() {
       //Do some more stuff
   }, 5000);
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Thats because in you are only going to replay the Timeout only when the user presses the "a" key. I don't know why you kept it like that but thats probably the reason.!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda