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

174
Views
Is it possible to stop a loop with another function in Javascript?

I have a function that is continuously incrementing a value. I am trying to implement a reset button that would reset the value to 0 and prevent it from incrementing any more. However, the reset button only changes the value but the loop keeps on going. Here is the code that accomplishes that (the actual value that is increasing is not mentioned in this block, the counter variable is involved with the rate at which the value is increased):

var counter = 0;

function create() {
//creates stuff
}

var timer;


function loop() {
        create()
}

function counter() {
    clearInterval(timer)
    timer = setInterval(loop, 1000 / (2**counter))
}

And it works as expected. However, my reset button looks like this:

function reset() {
    counter = 0
    clearInterval(timer)
    timer = setInterval(loop, 0)

The counter just continues to increase at the same rate it was before the reset. Is it possible to terminate this process?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Can you try below?

function initCounter() { 
  clearInterval(timer);
  timer = setInterval(loop, 1000 / (2**counter));
} 

function reset() { 
  counter = 0;
  initCounter(); 
}

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!