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

338
Visualizações
setInterval() wait time is after the previous one was called or after it was done? / how to control waiting time between loop iteration?

I am trying to use setInterval to call an API that has rate limit for requests per seconds. I tried to use setInterval as a while loop, but I can't find info on when it starts the next round.

So if I do:

setInterval(() => {
    CallAPI();
    // Some proccessing
}, 300)

Will the next round start after the first one finishes or regardless of it? because order matter, and also timing can cause an error.

I can also try to do the same with setTimeout and a while loop like so:

while(true) {
setTimeout(() => {
    CallAPI();
    // do some proccessing
}), 300
}

But again, I am not sure when the next round of the loop will start.

And lastly I cheat my way through it and do

while(true) {
    await CallAPI();
    // Do proccessing
    await setTimeout(() => true, 300)
}

So what is the order for setTimeout() and setInterval() in a loop?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Just call it when it is done executing. No need for a loop.

function fakeCall () {
  return new Promise((resolve) => {
    window.setTimeout(resolve, Math.random() * 2);
  });
}



function next() {
  window.setTimeout( async function () {
    await fakeCall();
    console.log(Date.now());
    next();
  }, 3000);
}

next();

about 4 years ago · Juan Pablo Isaza Relatório

0

You'll probably want to set up your loop as follows:

var TO_asyncLoop = null;
function asyncLoop(fn, ms){
  clearTimeout(TO_asyncLoop);
  TO_asyncLoop = setTimeout(() => { fn() }, ms)
}

var i = 0;
function doSomething(){
  // do somthing
  console.log("hi", i++);

  // when this "something" is finished, do it again:
  asyncLoop(doSomething, 500)
}

doSomething()

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