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

189
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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