javascript - 定时器完成时需要通知,在下一个间隔之前
问题描述
我想在计时器完成时收到通知,而不仅仅是“OFFSET”,有人可以帮助我吗?谢谢。
编辑:所以该脚本基本上是一个计时器,假设每隔一小时又一分钟在我的不和谐频道上给我一个通知。现在我只收到一个通知,目标距离 X 分钟。当实际计时器完成时(在下一个间隔之前),我想要另一个通知。
编辑2:我不想创建另一个主题,所以任何人都可以帮我添加秒吗?现在是唯一的小时和分钟计时器,我想增加几秒钟。所以改为:20:50 我想要:20:50:20
var Discord = require("discord.js");
var bot = new Discord.Client();
var NOTIFY_CHANNEL;
bot.on('ready', () => {
NOTIFY_CHANNEL = bot.channels.cache.get('xxx'); // Channel to send notification
});
const START_DATE = '2020-08-15'; // Date used as the starting point for multi-hour intervals, must be YYYY-MM-DD format
const START_HOUR = 19; // Hour of the day when the timer begins (0 is 12am, 23 is 11pm), used with START_DATE and INTERVAL_HOURS param
const INTERVAL_HOURS = 1; // Trigger at an interval of every X hours
const TARGET_MINUTE = 1; // Minute of the hour when the chest will refresh, 30 means 1:30, 2:30, etc.
const OFFSET = 5; // Notification will warn that the target is X minutes away
const NOTIFY_MINUTE = (TARGET_MINUTE < OFFSET ? 60 : 0) + TARGET_MINUTE - OFFSET;
console.log('Notification sent');
const START_TIME = new Date(new Date(START_DATE).getTime() + new Date().getTimezoneOffset() * 60000 + START_HOUR * 3600000).getTime();
setInterval(function () {
var d = new Date();
if (Math.floor((d.getTime() - START_TIME) / 3600000) % INTERVAL_HOURS > 0) return; // Return if hour is not the correct interval
if (d.getMinutes() !== NOTIFY_MINUTE) return; // Return if current minute is not the notify minute
NOTIFY_CHANNEL.send('in: ' + OFFSET + 'minutes');
}, 60 * 1000); // Check every minute
bot.login('xxx');
解决方案
为此,您要删除
if (d.getMinutes() !== NOTIFY_MINUTE)
更改您已有的计时器
NOTIFY_CHANNEL.send('in: ' + OFFSET + '分钟');
至
if (d.getMinutes() == NOTIFY_MINUTE) NOTIFY_CHANNEL.send('in: ' + OFFSET + 'minutes');
并添加
if (d.getMinutes() == TARGET_MINUTE) NOTIFY_CHANNEL.send('in: ' + OFFSET + 'minutes');
在目标分钟通知,
这给我们留下了
setInterval(function () {
var d = new Date();
if (Math.floor((d.getTime() - START_TIME) / 3600000) % INTERVAL_HOURS > 0) return; // Return if hour is not the correct interval
// if (d.getMinutes() !== NOTIFY_MINUTE) return; // Return if current minute is not the notify minute
if (d.getMinutes() == NOTIFY_MINUTE) NOTIFY_CHANNEL.send('in: ' + OFFSET + 'minutes');
if (d.getMinutes() == TARGET_MINUTE) NOTIFY_CHANNEL.send("It's now");
}, 60 * 1000); // Check every minute
推荐阅读
- c++ - 为什么在一个类的实例中转换一个变量也会在父类的另一个实例中转换它?
- mongodb - 带有字段存在检查的 MongoDB 项目
- node.js - Node js 和 mongodb 连接服务器失败 [70.32.xx.xx:27017
- c# - C# 核心 - Task.Wait(int Timeout) 未按预期等待
- python - Pyspark,先知,熊猫 UDF - [8906 行 x 3 列] 类型
. 对于列文字,使用 ' - axios - 如何使用 Axios 调用多个请求并使用 URL 数组连接答案
- swift - 斯威夫特 | macOS | 用 ChangeCount 上的新 NSPasteboard 内容更新 var
- ios - 在 iOS 上使用 PDFKit.PDFView 打开 PDF
- python - 使用 BeautifulSoup 在 Python 上不断变化的值打印为“SYNCHRONIZING”
- javascript - 递归:在变量中存储多个值