Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!