Is there any way I can execute the function at the exact minute (1 and 31 minutes for example ) every hour?
For example, it should be executed at:
08:01
08:31
09:01
09:31
10:01
...
Is there any way I can execute the function at the exact minute (1 and 31 minutes for example ) every hour?
For example, it should be executed at:
08:01
08:31
09:01
09:31
10:01
...
3
Answers
You could have a look at something like Croner, this can evaluate and run Cron expressions.
This package can also return a list of the next N runtimes for a cron expression.
Crontab.guru is handy for parsing these expressions too.
For your example, the cron expression will look like `1,31 * * * *’, meaning it will run at every minute and at thirty-one minutes past every hour.
You can use a
cron
to do that, or do it from scratch with asetInterval
to create a timer that will execute a callback everyn
millisecond (in your case every 60000).Then in the callback you will only have to check the value of
new Date().getMinutes()
(1 or 31) and execute the function if the condition passIf you want the function to be called at exactly 0 second, you can start with a
setInterval
executing the callback every second until the next minute, then use the above methodFor example:
Basically if you use setInterval or any other javascript package, keep it in mind that it will work only when the page is opened. Otherwise it will not work.
So, let say in your application, you have subscription system. Now whether user has opened the application or not, you have to expired subscription after specific time period. So, for these cases, you can’t use javascript.
Now for these cases, you need corn job. You can also use Database Triggers.