Appearance
node-cron
定时器对时区有很强的依赖性,如果你只需要每分钟或者每小时执行,那么时区可以不需要,但是一旦设置 某个小时或者某天某时,就需要设置时区,否则执行结果并非预期
- 语法
# ┌────────────── second (optional)
# │ ┌──────────── minute
# │ │ ┌────────── hour
# │ │ │ ┌──────── day of month
# │ │ │ │ ┌────── month
# │ │ │ │ │ ┌──── day of week
# │ │ │ │ │ │
# │ │ │ │ │ │
# * * * * * *
- 每分钟执行一次
js
import cron from "node-cron";
cron.schedule("* * * * *", () => {
console.log("running a task every minute");
});
- 每天零点执行一次
js
import cron from "node-cron";
cron.schedule(
"0 0 * * *",
() => {
console.log("running a task every day at 0 hour");
},
{ timezone: "Asia/shanghai" },
);
- scheduled
Starts the scheduled task.
js
import cron from "node-cron";
const task = cron.schedule(
"* * * * *",
() => {
console.log("stopped task");
},
{
scheduled: false,
},
);
task.start();