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

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

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

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