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

271
Visualizações
How to execute a function periodically in NodeJS

I already have tried setInterval(), it seemed perfect but the issue is that it does not initiate the first call immediately and waits for X seconds and then it gives desired value, is there some other way or should I run it an infinite loop and put a sleep like functionality in it?

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

0

If your issue is simply it doesn't call for first time then it is OK

function doThis(){
console.log("hello");
}
const time = 1000;
doThis(); // Calls for the first time
setInterval(doThis(),time); // Now it starts the samething.


//There is another option also for same thing:

setInterval(function doThis() {
  console.log('hello');
  return doThis();
}(), time);


But It is not recommended if function takes longer than delayed time More on this statement: https://dev.to/akanksha_9560/why-not-to-use-setinterval--2na9

https://chrisblackwell.me/setinterval-is-bad/

function doThis() {
console.log("hellow");

  setTimeout(doThis, 1500);
}
doThis();

The problem that happens with setInterval doesn't happen here, because this only schedules the next iteration, not all future ones.

For setInterval enter image description here For setTimeout enter image description here

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