node.js - 如何在固定日期前 24 小时在节点 js 中发送自动电子邮件?
问题描述
我想在我的节点应用程序中实现一个设置,服务器可以在特定日期前 24 小时向许多人发送电子邮件。该日期来自 DB。
例如:日期是 12 月 25 日,所以我需要我的服务器在 12 月 24 日向一组客户发送邮件。我今天如何对其进行编码,以便将来发送邮件?
(我的节点应用程序托管在 VPS 上,因此它将始终运行。)
解决方案
您可以使用节点调度模块。https://www.npmjs.com/package/node-schedule
var schedule = require('node-schedule');
var date = new Date(2020, 12, 24, 5, 30, 0);
var j = schedule.scheduleJob(date, function(y){ });
或者你可以用老式的方式来做。就像是。
const then = "2020-12-24 05:00:00";
const inter = setInterval(() => {
const now = new Date();
if (Date.parse(now) > Date.parse(then)) {
clearInterval(inter);
}
}, 1000 * 60 * 60 );
编辑:
你可以有类似的东西。
const schedules = {};
function dbUpdate() {
const somedate = new Date();
const userid = getUpdatedUser();
cancelSchedule(userid);
createSchedule(userid, somedate);
}
function createSchedule(userid, somedate) {
const date = new Date(somedate);
schedules[userid] = schedule.scheduleJob(date, function(y){ });
}
function cancelSchedule(userid) {
schedules[userid].cancel();
}
未经测试,但沿着这些路线
推荐阅读
- authentication - Groovy URL getText() 返回一个 PasswordAuthentication 实例
- elasticsearch - elasticsearch connectionTimeout 即使设置 timeout=100
- mysql - 未找到列:1054 未知列“playlist_entity_assets.play_list_id”
- sql - ROW_Number 与自定义组
- excel - 如何定义具有多列的名称标题
- c# - c# sort datetime list 获取最新时间
- wordpress - 如何构建自定义插件以与当前主题集成
- angular - 处理大型数据集时无法在 mat-paginator 中设置初始 pageIndex
- python - 如何解决“拒绝执行‘常量’语句”错误?
- html - CSS div float:left 问题,列有不同的高度