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?
- 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
3 Answers
Reset to default 2This 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
评论列表(0条)