博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于nodejs实现每天固定时间发送邮件服务
阅读量:6861 次
发布时间:2019-06-26

本文共 2997 字,大约阅读时间需要 9 分钟。

1.需求背景

实现一个每天固定时间(比如每天上午 10:00 )发送邮件的功能。

2.实现思路

比如要实现每天上午 10:00 发送邮件。

基本的思路就是写一个定时器,不断的去检测时间是否为10:00,是的话就触发。

假设距离 10:00 的秒数为 x,

当 x > 1 小时时,每隔 1 小时触发一个检测

当 x < 1 小时 && x > 10 分钟时,每隔 10 分钟触发一次检测

当 x < 10 分钟 && x > 1 分钟时,每隔 1 分钟触发一次检测

当 x < 1 分钟时,每隔 1 秒钟触发一次检测

3.实现代码如下

--demo  -index.js // 调用 timersService  -timersService.js // 核心代码复制代码

demo/index.js

const timersService = require('./timersService').timersService;const sendNotification = async () => {  console.log('------------------------------------------------');  console.log('发送邮件通知!(发送邮件的逻辑放在这儿!)');  console.log('------------------------------------------------');};// 每天 10:00 定时发送邮件timersService(10, 0, 0, sendNotification);复制代码

demo/timersService.js

const moment = require('moment');function execWhenTime(hour, minute, second, fn) {  const seconds = hour * 3600 + minute * 60 + second;  let timer = null;  let timerValue = null;  let currentH;  let currentM;  let currentS;  let currentSeconds;  let active = false;  const timerFn = () => {    currentH = parseInt(moment().format('HH'), 10);    currentM = parseInt(moment().format('mm'), 10);    currentS = parseInt(moment().format('ss'), 10);    currentSeconds = currentH * 3600 + currentM * 60 + currentS;    if (currentSeconds - seconds >= 0 && currentSeconds - seconds <= 3600 && !active) {      clearInterval(timer);      fn();      timerValue = 3600001;      timer = setInterval(timerFn, timerValue);      active = true;    }    const remainSeconds = seconds - currentSeconds >= 0 ? seconds - currentSeconds : seconds - currentSeconds + 24 * 3600;    if (seconds - currentSeconds > 3610 || seconds - currentSeconds < 0) {      console.log(`每小时检查一次, 还剩: ${(remainSeconds / 3600).toFixed(2)} 小时触发定时通知!`);      // 每小时执行一次      if (timerValue !== 3600000) {        active = false;        timerValue = 3600000;        clearInterval(timer);        timer = setInterval(timerFn, timerValue);      }    } else {      if (seconds - currentSeconds > 610) {        console.log(`每10分钟检查一次, 还剩: ${remainSeconds} 秒触发定时通知! `);        // 每10分钟执行一次        if (timerValue !== 600000) {          timerValue = 600000;          clearInterval(timer);          timer = setInterval(timerFn, timerValue);        }      } else {        if (seconds - currentSeconds > 65) {          console.log(`每1分钟检查一次, 还剩: ${remainSeconds} 秒触发定时通知!`);          // 每分钟执行一次          if (timerValue !== 60000) {            timerValue = 60000;            clearInterval(timer);            timer = setInterval(timerFn, timerValue);          }        } else {          console.log(`每秒钟检查一次, 还剩: ${remainSeconds} 秒触发定时通知!`);          // 每秒钟执行一次          if (timerValue !== 1000) {            timerValue = 1000;            clearInterval(timer);            timer = setInterval(timerFn, timerValue);          }        }      }    }  };  timerFn();}exports.timersService = (hour, minute, second, fn) => {  execWhenTime(hour, minute, second, fn);};复制代码

4.Code

github代码: https://github.com/SimpleCodeCX/myCode/tree/master/execWhenTime

转载地址:http://esayl.baihongyu.com/

你可能感兴趣的文章
《OOD启思录》—本书中引用到的其他图书
查看>>
网站建设前要注意这些网站设计误区可能会毁了网站
查看>>
《乐高EV3机器人搭建与编程》一1.2 LEGO系列产品
查看>>
《HTML、CSS、JavaScript 网页制作从入门到精通》——6.3 表格的边框
查看>>
《Spring攻略(第2版)》——1.9 用依赖检查属性
查看>>
并发集合(七)创建并发随机数
查看>>
论文导读:面向卷积神经网络的卷积核冗余消除策略
查看>>
当下流行架构中的一些技术思考
查看>>
Hadoop学习第四天之hadoop命令操作(上)
查看>>
走进阿里云:做云数据、大计算的No.1
查看>>
Gradle 基础
查看>>
listview优化(中)
查看>>
当安全遇上AI 阿里聚安全算法挑战赛完美收官
查看>>
怪”博士闵万里:用人工智能,解决吃饭出行问题
查看>>
ES6 + Webpack + React + Babel 如何在低版本浏览器上愉快的玩耍(下)
查看>>
日志服务(原SLS)新功能发布(8)--日志服务Web Tracking功能
查看>>
kvm虚拟化学习笔记(十三)之kvm虚拟机磁盘文件读取小结
查看>>
kvm虚拟化学习笔记(四)之kvm虚拟机日常管理与配置
查看>>
SlideView 图片滑动(扩展/收缩)展示效果
查看>>
iOS开发之即时通讯之Socket(AsyncSocket)
查看>>