javascript - Creating a cron job using a while loop - Stack Overflow

There are several libraries (especially for NodeJS and Javascript) that allow you to implement cron job

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?

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?
Share Improve this question asked Apr 27, 2017 at 20:07 WJAWJA 7,00420 gold badges99 silver badges170 bronze badges 4
  • Because this is just an infinite loop. Good luck with making this work. Javascript is asynchronous. This piece of code just sets an infinite number of timeouts, and it will fast crash your browser. Look for setInterval instead. – Jeremy Thille Commented Apr 27, 2017 at 20:17
  • Thats my question, isnt an infinite loop the same as what a cron job does? – WJA Commented Apr 27, 2017 at 20:20
  • No because a cron job works, whereas an infinite loop crashes the script and often the tab/browser. – Jeremy Thille Commented Apr 27, 2017 at 20:25
  • What makes it work? Thats what I am not getting. You mean an infinite loop results in a memory overload or what? – WJA Commented Apr 27, 2017 at 20:29
Add a ment  | 

3 Answers 3

Reset to default 2

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

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?

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745235471a4617870.html

相关推荐

  • javascript - Creating a cron job using a while loop - Stack Overflow

    There are several libraries (especially for NodeJS and Javascript) that allow you to implement cron job

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信