zoukankan      html  css  js  c++  java
  • nodejs定时任务node-schedule

    1:使用npm安装node-schedule模块

    npm install node-schedule

    (1)每隔5分钟执行一次:

    var schedule = require('node-schedule');
    
    var rule = new schedule.RecurrenceRule();
    
    rule.minute = [0,5,10,15,20,25,30,35,40,45,50,55];
    
    var j = schedule.scheduleJob(rule,function(){
        console.log("执行任务:"+new Date());
    });

     (2)上午8点到晚上20点每隔5分钟执行一次:

    var schedule = require('node-schedule');
    
    var rule = new schedule.RecurrenceRule();
    
    rule.hour = [8,9,10,11,12,13,14,15,16,17,18,19,20];
    rule.minute = [0,5,10,15,20,25,30,35,40,45,50,55];
    
    var j = schedule.scheduleJob(rule,function(){
        console.log("执行任务:"+new Date());
    });

    2:以下内容参考:

    http://www.codexpedia.com/javascript/nodejs-cron-schedule-examples/

    Using the node-schedule to schedule a job to run at a specific time on a specific date. As the first example, the node-schedule module is imported and save it in the variable cron. In the following examle, the require statement will be ommitted and this variable cron will be used.

    1
    2
    3
    4
    5
    6
    var cron = require('node-schedule');
    /* run the job at 18:55:30 on Dec. 14 2018*/
    var date = new Date(2018, 11, 14, 18, 56, 30);
    cron.scheduleJob(date, function(){
        console.log(new Date(), "The world is going to end today.");   
    });

    Schedule a recurring job using the RecurrenceRule, example 1.

    1
    2
    3
    4
    5
    var rule = new cron.RecurrenceRule();
    rule.second = 30;
    cron.scheduleJob(rule, function(){
        console.log(new Date(), 'The 30th second of the minute.');
    });

    Schedule a recurring job using the RecurrenceRule, example 2.

    1
    2
    3
    4
    5
    6
    7
    8
    /* This runs at 3:10AM every Friday, Saturday and Sunday. */
    var rule2 = new cron.RecurrenceRule();
    rule2.dayOfWeek = [5,6,0];
    rule2.hour = 3;
    rule2.minute = 10;
    cron.scheduleJob(rule2, function(){
        console.log('This runs at 3:10AM every Friday, Saturday and Sunday.');
    });

    Specify the schedule as an object literal.

    1
    2
    3
    4
    /* This runs at 2:30AM on every Sunday */
    cron.scheduleJob({hour: 2, minute: 30, dayOfWeek: 0}, function(){
        console.log('This runs at 2:30AM on every Sunday');
    });

    Specify the schedule in unix cron syntax.

    1
    2
    3
    4
    /* This runs at the 30th mintue of every hour. */
    cron.scheduleJob('30 * * * * *', function(){
        console.log('This runs at the 30th mintue of every hour.');
    });
  • 相关阅读:
    JAVA入门[7]-Mybatis generator(MBG)自动生成mybatis代码
    JAVA入门[6]-Mybatis简单示例
    JAVA入门[5]-初步搭建SpringMVC站点
    欠阿里云一分钱,会是什么样的后果
    IIS虚拟目录挂载文件服务器目录
    “Validation failed for one or more entities”异常的解决办法
    idapython import 'site' failed
    .Net Core 2.0 EntityFrameworkCore CodeFirst入门教程
    MVC添加Area出现“到多个与名为“Home”的控制器匹配的类型的解决方法”
    VS 2017 Git failed with a fatal error的解决办法
  • 原文地址:https://www.cnblogs.com/yshyee/p/4465741.html
Copyright © 2011-2022 走看看