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

282
Views
Count down with setInterval has time difference
let count = 60;

function timeLeft(){
    count = count - 1;
    if(count == 0){
        count = 60;
    }
}

setInterval(() => timeLeft(), 1000)
setInterval(() => machoAirline.changePrice(), 60000);
setInterval(() => yenoTech.changePrice(), 60000);

This code is the countdown for showing how much time is left until stocks' prices change. But after some minutes, there is a gap between counter and prices update.

console:

price updated!
counter: 42

What's the problem?

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

0

Timeout and intervals may take longer to fire when you want due to:

  • inactive of tabs
  • throttling of tracking scripts
  • Late timeouts because browser busy with other tasks.

If you need correct order of machoAirline and yenoTech execution you can combine them in one setInterval:

setInterval(() => {
  machoAirline.changePrice();
  yenoTech.changePrice();
}, 60000);
about 4 years ago · Juan Pablo Isaza Report

0

setInterval does not keep accurate time, as described in other answers. This means that calculating elapsed time by counting the invocations of setInterval will result in inaccurate timings when compared to wall-time (or indeed, other intervals that you have set).

performance.now() is the most accurate measure of time available in the browser.

If timing is critical, then consider polling performance.now() in the body of a setInterval callback that runs at a short interval...

Something like:

const now = performance.now();
const finishTime = now + 3000; // 3s time

const handle = setInterval(() => {
  if (performance.now() >= finishTime) {
    console.log("some action");
    clearInterval(handle); // kill the timer
  }
}, 100); // poll every 100ms (ish)

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!