在项目中我们有时需要在指定的时间来执行一定的时间,比如每分钟10s的时候提示一下
先安装需要的包
npm install node-schedule
插件的格式介绍
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun) 星期
│ │ │ │ └───── month (1 - 12) 月
│ │ │ └────────── day of month (1 - 31) 日
│ │ └─────────────── hour (0 - 23) 时
│ └──────────────────── minute (0 - 59) 分
└───────────────────────── second (0 - 59, OPTIONAL) 秒
设置时间
var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
rule.second = 10 // 设置你需要响应的时间
var j = schedule.scheduleJob(rule, function(){
console.log("每分钟10s的时候提示一下");
})
运行一下,运行一下,然后耐心等待就看的出效果了
git地址: https://github.com/node-schedule/node-schedule
如有疑问或补充请提出来。
转载请附上链接,谢谢