node-schedule nodejs定时提醒(并判断段是否是工作日)

概述 工作中有个需求:在特定的时间发送一些消息,也就是说比如在每天的7点发送消息:该起床了。一开始我想用定时器每分钟每分钟的去查当前时间,但好像有点蠢

概述

工作中有个需求:在特定的时间发送一些消息,也就是说比如在每天的7点发送消息:该起床了。一开始我想用定时器每分钟每分钟的去查当前时间,但好像有点蠢,然后我找到了这个包

使用方法

安装

npm install node-schedule

使用

const schedule = require('node-schedule');

schedule.scheduleJob('* * * * * *', function () {
  console.log('111111');
});

这里的每个*都可以被换掉,比如你要每个整点都执行一次,这段你就写成 0 * * * *
可以有缺省,默认缺省的都在最左边(秒 分 时 天 月 年

判断工作日

这里我采用的是chinese-workday来判断是否是工作日使用方法参考

const schedule = require('node-schedule');
const ww = require('chinese-workday');
const sendMessage = require('../util/sendMessage');
const sys_user = require('../db/model/sys_user');

module.exports.msgReminder = async () => {
  let users = await sys_user.findAll({ raw: true });
  schedule.scheduleJob('00 19 * * *', async function () {
    if (ww.isWorkday(new Date())) {
      for (let user of users) {
        let msg = '提醒一下,要xxxxxx了';      
          sendMessage(msg, user.open_id);
      }
    }
  });
};

发布者:admin,转转请注明出处:http://www.yc00.com/web/1755001409a5225553.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信