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

228
Views
cannot clear Interval within if query

I am learning about the setInterval and clearInterval method and meanwhile there appeared some problems with my learning process. In the code below you can see the function countdown which is including the var y which should be increased after every 500 milliseconds. If the var y reaches the value 3 the Interval should be cleared. But my code is not working like I actually want. What I am doing wrong?

  let y = 1
function countdown() {
    
    y += 0.5
    console.log(y)
}

const timerId = setInterval(countdown, 500)

if( y > 3) {
    clearInterval(timerId);
}

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

0

You need to put the ( y > 3) check within the countdown function. Currently it'll only be executed once, and at the end.

Here's a working example:

let y = 1;

const countdown = () => {
  y += 0.5;
  console.log(y);
  if( y >= 3) {
    clearInterval(timerId);
  }
}

const timerId = setInterval(countdown, 500);

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!