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.');
});

nodejs定时任务node-schedule的更多相关文章

  1. 基于agenda的Nodejs定时任务管理框架搭建

    0.背景 在大型项目中,定时任务的应用场景越来越广.一般来说,按照微服务的思想,我们会将定时任务单独部署一套服务,核心的业务接口独立到另一个服务中,从而降低相互之间的耦合程度.在需要使用定时任务时,只 ...

  2. NodeJS定时任务

    在实际开发项目中,会遇到很多定时任务的工作.比如:定时导出某些数据.定时发送消息或邮件给用户.定时备份什么类型的文件等等 一般可以写个定时器,来完成相应的需求,在node.js中自已实现也非常容易,接 ...

  3. NodeJS(node.exe, npm, express, live-server)安装

    1.下载node.exe 下载https://nodejs.org/en/download/current/ 创建D:\GreenSoftware\NodeJS目录,并将node.exe放到目录中. ...

  4. [Nodejs] 用node写个爬虫

    寻找爬取的目标 首先我们需要一个坚定的目标,于是找个一个比较好看一些网站,将一些信息统计一下,比如 url/tag/title/number...等信息 init(1, 2); //设置页数,现在是1 ...

  5. 【NodeJS】Node.JS 开发环境安装

    1.前言 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一个事件驱动I/O ...

  6. Nodejs:Node.js模块机制小结

    今天读了<深入浅出Nodejs>的第二章:模块机制.现在做一个简单的小结. 序:模块机制大致从这几个部分来讲:JS模块机制的由来.CommonJS AMD CMD.Node模块机制和包和n ...

  7. Nodejs 定时任务

    安装扩展:node-schedule npm install node-schedule 1.linux Crontab风格 var schedule = require('node-schedule ...

  8. SpringBoot系列:Spring Boot定时任务Spring Schedule

    Spring Schedule是Spring提供的定时任务框架,相较于Quartz,Schedule更加简单易用,在中小型应用中,对于大部分需求,Schedule都可以胜任. 一.Spring Sch ...

  9. 定时任务模块 schedule

    # coding:utf-8 from learning_python.Telegram_push.check_hardware import check_cpu import schedule im ...

随机推荐

  1. Selenium webdirver 操作浏览器

    打开浏览器 HtmlUnit Driver 优点:不会实际打开浏览器,运行速度很快. 缺点:对JavaScript的支持不够好,有时会捕获不到页面元素. 使用:WebDriver driver=new ...

  2. 【Cocos2d-X开发学习笔记】第10期:渲染框架之几何图形的绘制

    本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010 一.图形的绘制 有时为了调试游戏,我们常常会需要 ...

  3. java.lang.UnsatisfiedLinkError: Native method not found 三种可能解决方案

    so文件编译生成后,运行时,有时候会遇到java.lang.UnsatisfiedLinkError: Native method not found问题,有可能是以下三种因素: 一.Jni方法头部大 ...

  4. [Angular 2] Child Router

    Benefit to use child router is Angualr 2 then can lazy load the component on demand. Define a child ...

  5. 05-图2. Saving James Bond - Easy Version (25)

    1 边界和湖心小岛分别算一个节点.连接全部距离小于D的鳄鱼.时间复杂度O(N2) 2 推断每一个连通图的节点中是否包括边界和湖心小岛,是则Yes否则No 3 冗长混乱的函数參数 #include &l ...

  6. MSSQL row_number简单使用语法

    MSSQL row_number简单使用语法 select * from ( select row_number() over(partition by threadid order by date ...

  7. Java基础知识强化90:Date类之Data类中日期和毫秒相互转换

    1.Date两个方法: public long getTime():获取时间,以毫秒为单位 public void setTime(long time):设置时间 2. 代码示例: package c ...

  8. RollPagerView的用法:

    RollPagerView的用法: /** * * @author smiling * @date 2016/10 */ Android Studio 导包: compile 'com.jude:ro ...

  9. automatically select architectures

    各位在用XCode 5.x 打开用XCode 4.x 创建的项目时候.会遇到编译器警告automatically select architectures. 1. This is because th ...

  10. 九度OJ 1552座位问题(dp)

    刚开始写的一维数组,但是由于后面的数字较大,要对它取模,所以用一维数组进行减法运算,结果就不对了.所以还是得用二维数组,用dp[n][0]来表示第n位为男生总的方法个数,dp[n][1]表示第n位为女 ...