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

373
Visualizações
Creating a cron job using a while loop

There are several libraries (especially for NodeJS and Javascript) that allow you to implement cron jobs and subsequently host them on a server.

In essence, cron jobs seem to me nothing more than repetitive tasks that are executed at a specific time/date on a day.

I was wondering therefore what the difference is between these libraries and just let's say a custom while loop. For instance in Javascript we could write:

var keepRunning = true
while (keepRunning) {
    setTimeout(function () {
        // call function to be executed when time constraint satisfied
    }, 5000);
}

My questions are therefore:

  • why do we use cron job libraries? What are the benefits above a custom function like above?
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

This would not work as you might expect:

var keepRunning = true
while (keepRunning) {
    setTimeout(function () {
        // call function to be executed when time constraint satisfied
    }, 5000);
}

That code will schedule new setTimeout callbacks as fast as it can while keepRuning is true, never unwinding the call stack and letting the event loop run any of those callbacks. It will likely consume all of your memory without running the scheduled code even once.

What you can do is something like this:

var keepRunning = true;
function run() {
  if (keepRunning) {
    // call function to be executed when time constraint satisfied
    setTimeout(run, 5000);
  }
}
setTimeout(run, 5000);

If you want to schedule all the callbacks at once, then you might do something like this:

for (let i = 1; i <= 100; i++) {
    setTimeout(function () {
        // call function to be executed when time constraint satisfied
    }, 5000 * i);
}

but in this example you need to multiply the timeout by the iteration variable to make sure that they are not scheduled to run at the same time - i.e. they are still scheduled all at once but they are later run at different times.

Remember that JavaScript runs to completion and callbacks are executed later when the call stack unwinds. It's also important that for and while loops block the event loop from executing and no event can be handled while the loop is running.

over 4 years ago · Santiago Trujillo Relatório

0

Cron handles very time specific events far better than this. If you wanted something to happen at 9am each day you would absolutely have to use Cron over some method like this.

Cron measures time from epoch and is the most accurate way of scheduling tasks. I would also imagine it would result in better performance than what you are suggesting.

Why would you NOT use Cron?

over 4 years ago · Santiago Trujillo Relatório

0

A library is a collection of useful code. Those collections tend to group up a significant amount of functions, objects, etc.

Your example was just one situation that had very little versatility. Libraries would provide much more options for your loop, and would go beyond just addressing the rate, but also other factors (depending on what specific library you are referring to).

over 4 years ago · Santiago Trujillo 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