node.js中使用node-schedule實現(xiàn)定時任務實例
有的時候需要根據(jù)業(yè)務需要,晚上凌晨以后執(zhí)行某些操作的時候,這個可能會有所幫助,我最近正在研究這個,歡迎共同探討。
github地址:https://github.com/mattpat/node-schedule
一、安裝
npm install node-schedule
二、確定時間,例如:2012年11月21日,5:30
var schedule = require('node-schedule');
var date = new Date(2012, 11, 21, 5, 30, 0);
var j = schedule.scheduleJob(date, function(){
console.log('The world is going to end today.');
});
取消預設計劃
[code]
j.cancel();
三、每小時的固定分鐘,例如:每個小時的42分鐘
var schedule = require('node-schedule');
var rule = new schedule.RecurrenceRule();
rule.minute = 42;
var j = schedule.scheduleJob(rule, function(){
console.log('The answer to life, the universe, and everything!');
});
四、.一個星期中的某些天的某個時刻,例如:每周四,周五,周六,周天的17點
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(4, 6)];
rule.hour = 17;
rule.minute = 0;
var j = schedule.scheduleJob(rule, function(){
console.log('Today is recognized by Rebecca Black!');
});
五、每秒執(zhí)行
var rule = new schedule.RecurrenceRule();
var times = [];
for(var i=1; i<60; i++){
times.push(i);
}
rule.second = times;
var c=0;
var j = schedule.scheduleJob(rule, function(){
c++;
console.log(c);
});
相關文章
node.js使用express-fileupload中間件實現(xiàn)文件上傳
本文使用express作為服務端,使用express-fileupload庫提供的中間件函數(shù)來接受從客戶端傳來的圖片,并將圖片作為文件存儲在服務端,感興趣的可以了解一下2021-07-07
10個Node.js庫幫助你優(yōu)化代碼和簡化開發(fā)
這篇文章主要介紹了10個Node.js庫幫助你優(yōu)化代碼和簡化開發(fā),其中包括處理數(shù)組、對象、字符串庫Lodash,緩存數(shù)據(jù)處理庫Node-cache,解析、操作和格式化日期和時間庫Moment.js,Redis操作庫,發(fā)送電子郵件庫Nodemailer2023-05-05
如何在Node.js中使用async函數(shù)的方法詳解
這篇文章主要為大家介紹了如何在Node.js中使用async函數(shù)的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
npm報錯:request to httpsregistry.npm.taobao.org 
這篇文章主要介紹了npm報錯:request to httpsregistry.npm.taobao.org failed, reason certificate has expired的解決方案,文中有詳細的解決方案,需要的朋友可以參考下2024-03-03
Node.js連接mongo數(shù)據(jù)庫上傳文件的方法步驟
本文主要介紹了Node.js連接mongo數(shù)據(jù)庫上傳文件的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05

