node定时任务cron的使用

可以方便的配置定时任务的执行时间

var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
  console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');

可以方便的设置调度任务,使用方法如下:

var cron = require('cron');
 
var job1 = new cron.CronJob({
  cronTime: '* * * * * *',        // pre second
  onTick: function() {
    console.log('job 1 ticked');
  },
  start: false,
  timeZone: 'America/Los_Angeles'
});
 
var job2 = new cron.CronJob({
  cronTime: '*/5 * * * * *',     // every 5 seconds 
  onTick: function() {
    console.log('job 2 ticked');
  },
  start: false,
  timeZone: 'America/Los_Angeles'
});
 
console.log('job1 status', job1.running); // job1 status undefined
console.log('job2 status', job2.running); // job2 status undefined
 
job1.start(); // job 1 started
job2.start(); // job 2 started
 
console.log('job1 status', job1.running); // job1 status true
console.log('job2 status', job2.running); // job2 status undefined
  • 参考连接: http://www.cnblogs.com/junrong624/p/4239517.html

你可能感兴趣的:(node定时任务cron的使用)