Code ví dụ Node.js tạo lịch với module node-schedule. [Xem thêm: Tự học Node.js qua ví dụ] Node schedule là một module dùng để đặt lịch chạy cho một chức năng nào đó, ví dụ 5 giờ sáng hàng ngày chạy chức năng báo thức, cứ 7h sáng chủ nhật hàng tuần gửi thông báo… Cài đặt module Include module node-schedule vào project Ví dụ: Trong ví dụ trên các bạn thấy node-schedule
:npm install node-schedule
var schedule = require['node-schedule'];
var schedule = require['node-schedule'];
var cronExpress = '*/5 * * * * * *';
var j = schedule.scheduleJob[cronExpress, function[fireDate]{
console.log['running job!'];
console.log[fireDate]
}];
scheduleJob
thực hiện chạy function theo lịchvar schedule = require['node-schedule'];
0 là cron expression thực hiện định nghĩa lịch chạy [chạy vào thời điểm nào, sau bao lâu thì lặp lại…]var schedule = require['node-schedule'];
1 là thời điểm function được chạy.Cron Expression, Cron-style trong module node-schedule
var schedule = require['node-schedule'];
2 tức là cứ 5 giây thì lặp lại một lần.
Cấu trúc, cú pháp của cron expression:
Dấu * biểu thị có thể nhận bất kì giá trị nào, dấu / thể hiện lặp lại
Ví dụ:
- function
scheduleJob
thực hiện chạy function theo lịch var schedule = require['node-schedule'];
0 là cron expression thực hiện định nghĩa lịch chạy [chạy vào thời điểm nào, sau bao lâu thì lặp lại…]- Tham số
var schedule = require['node-schedule'];
1 là thời điểm function được chạy. - Cron Expression, Cron-style trong module node-schedule
Trong ví dụ trên các bạn thấy var schedule = require['node-schedule'];
2 tức là cứ 5 giây thì lặp lại một lần.
Cấu trúc, cú pháp của cron expression:
Ví dụ:
var schedule = require['node-schedule']; var date = new Date[2018, 9, 11, 12, 0, 0]; console.log[date]; var j = schedule.scheduleJob[date, function[]{ console.log['hello world.']; }];
function scheduleJob
thực hiện chạy function theo lịch
var schedule = require['node-schedule'];0 là cron expression thực hiện định nghĩa lịch chạy [chạy vào thời điểm nào, sau bao lâu thì lặp lại…]
Tham số var schedule = require['node-schedule'];
1 là thời điểm function được chạy.
Cron Expression, Cron-style trong module node-schedule
Trong ví dụ trên các bạn thấy
var schedule = require['node-schedule'];2 tức là cứ 5 giây thì lặp lại một lần.
var schedule = require['node-schedule']; var rule = new schedule.RecurrenceRule[]; rule.minute = 10; var j = schedule.scheduleJob[rule, function[]{ console.log['hello world']; }];
Cấu trúc, cú pháp của cron expression:
Dấu * biểu thị có thể nhận bất kì giá trị nào, dấu / thể hiện lặp lại
Cứ 5 giây lặp lại 1 lần:
var schedule = require['node-schedule'];3
var j = schedule.scheduleJob[{hour: 8, minute: 30}, function[]{ console.log['Time for work!']; }];
Cứ 5 giờ hàng ngày thì lặp lại: var schedule = require['node-schedule'];
4
Cứ 8h 5 phút từ thứ 2 đến thứ 6 thì lặp lại:
var schedule = require['node-schedule'];5
Từ phút thứ 2 đến phút thứ 10, cứ 2 phút lặp lại 1 lần:
var schedule = require['node-schedule'];6
let startTime = new Date[2018, 9, 11, 12, 0, 0]; let endTime = new Date[2018, 9, 11, 12, 1, 0]; var j = schedule.scheduleJob[{ start: startTime, end: endTime, rule: '*/1 * * * * *' }, function[]{ console.log['Time for tea!']; }];
Date-based Scheduling
Ngoài cách dùng cron expression ta có thể thay bằng date để chỉ rõ thời gian chạy.
Đoạn code trên thực hiện log ra dòng chữ
var schedule = require['node-schedule'];7 vào lúc 12:00:00 ngày 11/10/2018.
[Trong đối tượng date, tháng tính từ 0 nên ở vị trí tháng mình để là 9 tức là tương ứng với tháng 10].
var schedule = require['node-schedule']; var cronExpress = '*/5 * * * * * *'; var j = schedule.scheduleJob[cronExpress, function[fireDate]{ console.log['running job!']; console.log[fireDate] }];
Recurrence Rule Scheduling
Một cách khác để định nghĩa lịch là sử dụng
var schedule = require['node-schedule'];8
Ví dụ: cứ vào phút thứ 10 thì in ra dòng chữ
var schedule = require['node-schedule'];9
References:
//www.npmjs.com/package/node-schedule