node.js - setInterval & setTimeout 增量重复命令
node.js - setInterval & setTimeout incrementally repeating command
我正在尝试帮助朋友为 Picarto.tv
设置一个机器人,我们有机器人 LINK 并且没有默认的 plugin
用于重复消息,所以我尝试制作一个非常粗糙的(说真的,这太糟糕了。我不是开发人员。)plugin
,我尝试使用 SetInterval/SetTimeout
,当我使用它们时,两次都会将消息放入聊天一次,在设置的间隔,然后它会等待,然后在间隔之后它会说消息两次,然后三次,依此类推。
看起来像这样:
Time 1:
Testing...
Time 2:
Testing...
Testing...
等等。这是代码,正如我所说,它制作得非常棒,不要bash我太难了。
var api;
function handleChatMsg(data) {
var recursive = function () {
api.Messages.send("Testing Bot Repeat...");
setTimeout(recursive,15000);
}
recursive();
}
module.exports = {
meta_inf: {
name: "Repeat Message",
version: "1.0.0",
description: "Repeats a message every 5 minutes. Message and interval can be changed.",
author: "ZX6R"
},
load: function (_api) {
api = _api;
},
start: function () {
api.Events.on("userMsg", handleChatMsg);
}
}
谁能帮我弄清楚为什么它会越来越多地重复这条信息?
编辑:问题已修复,新代码为
var api;
// Function to call for the repeating
function handleChatMsg() {
// This sets the interval of 5 minutes, and calls the variable. Edit the numbers after the comma to change the interval. You MUST put it into milliseconds.
setInterval(function(){xyz()}, 15000);
// This sets the variable, edit the text in "api.Messages.send" to change what the bot repeats.
var xyz = function()
{
api.Messages.send("Testing...")
}
}
// defines some information about the plugin, and sets up stuff we need.
module.exports = {
meta_inf: {
name: "Repeat Message",
version: "1.1.1",
description: "Repeats a message every 5 minutes. Message and interval can be changed.",
author: "ZX6R"
},
load: function (_api) {
api = _api;
},
start: function () {
handleChatMsg();
}
}
// The MIT License (MIT)
// Copyright (c) 2016 RedFalconv2 - ZX6R - WalnutGaming
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
让我们不要把事情复杂化,让它变得更简单。
setInterval(function(){xyz()}, 300000);
var xyz = function()
{
//Do what you want your function to do.
}
此处函数 xyz 将每 300,000 毫秒调用一次,即 5 分钟。
看看 Node Cron 顺便说一下,如果您要定期将其用于某个应用程序。
可能像下面这样:
// define and export module
var plugin = {};
module.exports = plugin;
// plugin metadata
plugin.meta_inf = {
name: "Repeat Message",
version: "1.0.0",
description: "Repeats a message every 5 minutes. Message and interval can be changed.",
author: "ZX6R"
};
/**
* To be called on plugin load.
* @param {Object} api - api instance.
*/
plugin.load = function(api){
plugin._api = api;
};
/**
* Called on plugin start.
*/
plugin.start = function(){
if(!plugin._api){
// api instance should have been available
throw new Error("api not defined, is plugin load()'ed ?");
}
// each user message event (re)configures the repeating timer
plugin._api.Events.on("userMsg",function(){
plugin._configure(true, "Testing echo bot...",15000);
});
};
/**
* Configure the repeating timer
* @param {Boolean} enabled - true to enable timer, false to disable
* @param {String} message - message to repeat, required if enabled
* @param {Number} interval - milliseconds between repeats, required if enabled
*/
plugin._configure = function(enabled, message, interval){
if(plugin._timer){
// always clear any old timer
clearInterval(plugin._timer);
}
if(enabled){
if(!message || !interval){
// message and interval are required
throw new Error("message and interval are required.");
}
// set a repeating timer
plugin._timer = setInterval(function(){
plugin._api.Messages.send(message);
},interval);
}
};
备注:
- 用户消息事件应用新的计时器设置。您可以让机器人重复用户消息,或自定义的内容。
- 或者,您只能配置一次定时器。
我正在尝试帮助朋友为 Picarto.tv
设置一个机器人,我们有机器人 LINK 并且没有默认的 plugin
用于重复消息,所以我尝试制作一个非常粗糙的(说真的,这太糟糕了。我不是开发人员。)plugin
,我尝试使用 SetInterval/SetTimeout
,当我使用它们时,两次都会将消息放入聊天一次,在设置的间隔,然后它会等待,然后在间隔之后它会说消息两次,然后三次,依此类推。
看起来像这样:
Time 1:
Testing...
Time 2:
Testing...
Testing...
等等。这是代码,正如我所说,它制作得非常棒,不要bash我太难了。
var api;
function handleChatMsg(data) {
var recursive = function () {
api.Messages.send("Testing Bot Repeat...");
setTimeout(recursive,15000);
}
recursive();
}
module.exports = {
meta_inf: {
name: "Repeat Message",
version: "1.0.0",
description: "Repeats a message every 5 minutes. Message and interval can be changed.",
author: "ZX6R"
},
load: function (_api) {
api = _api;
},
start: function () {
api.Events.on("userMsg", handleChatMsg);
}
}
谁能帮我弄清楚为什么它会越来越多地重复这条信息?
编辑:问题已修复,新代码为
var api;
// Function to call for the repeating
function handleChatMsg() {
// This sets the interval of 5 minutes, and calls the variable. Edit the numbers after the comma to change the interval. You MUST put it into milliseconds.
setInterval(function(){xyz()}, 15000);
// This sets the variable, edit the text in "api.Messages.send" to change what the bot repeats.
var xyz = function()
{
api.Messages.send("Testing...")
}
}
// defines some information about the plugin, and sets up stuff we need.
module.exports = {
meta_inf: {
name: "Repeat Message",
version: "1.1.1",
description: "Repeats a message every 5 minutes. Message and interval can be changed.",
author: "ZX6R"
},
load: function (_api) {
api = _api;
},
start: function () {
handleChatMsg();
}
}
// The MIT License (MIT)
// Copyright (c) 2016 RedFalconv2 - ZX6R - WalnutGaming
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
让我们不要把事情复杂化,让它变得更简单。
setInterval(function(){xyz()}, 300000);
var xyz = function()
{
//Do what you want your function to do.
}
此处函数 xyz 将每 300,000 毫秒调用一次,即 5 分钟。
看看 Node Cron 顺便说一下,如果您要定期将其用于某个应用程序。
可能像下面这样:
// define and export module
var plugin = {};
module.exports = plugin;
// plugin metadata
plugin.meta_inf = {
name: "Repeat Message",
version: "1.0.0",
description: "Repeats a message every 5 minutes. Message and interval can be changed.",
author: "ZX6R"
};
/**
* To be called on plugin load.
* @param {Object} api - api instance.
*/
plugin.load = function(api){
plugin._api = api;
};
/**
* Called on plugin start.
*/
plugin.start = function(){
if(!plugin._api){
// api instance should have been available
throw new Error("api not defined, is plugin load()'ed ?");
}
// each user message event (re)configures the repeating timer
plugin._api.Events.on("userMsg",function(){
plugin._configure(true, "Testing echo bot...",15000);
});
};
/**
* Configure the repeating timer
* @param {Boolean} enabled - true to enable timer, false to disable
* @param {String} message - message to repeat, required if enabled
* @param {Number} interval - milliseconds between repeats, required if enabled
*/
plugin._configure = function(enabled, message, interval){
if(plugin._timer){
// always clear any old timer
clearInterval(plugin._timer);
}
if(enabled){
if(!message || !interval){
// message and interval are required
throw new Error("message and interval are required.");
}
// set a repeating timer
plugin._timer = setInterval(function(){
plugin._api.Messages.send(message);
},interval);
}
};
备注:
- 用户消息事件应用新的计时器设置。您可以让机器人重复用户消息,或自定义的内容。
- 或者,您只能配置一次定时器。