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

372
Views
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 answers
Answer question

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 Report

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 Report

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