node.js - setInterval& setTimeout递增重复命令

时间:2016-07-18 04:32:34

标签: node.js settimeout setinterval

我正在尝试帮助朋友为Picarto.tv设置机器人,我们有机器人LINK并且没有默认plugin用于重复消息,所以我试图制作一个非常粗糙(严重的,这很糟糕。我不是开发人员。)plugin因为它,我尝试使用SetInterval/SetTimeout,并且两次当我使用它时它会将消息放入聊天中,在设定的时间间隔,然后它将等待,然后在间隔后,它将说明消息两次,然后三次,依此类推。

看起来像这样:

Time 1:  
Testing...  

Time 2:  
Testing...  
Testing...  

等等。这是代码,正如我所说的那样,它是非常糟糕的,不要因为它而过分抨击我。

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.

2 个答案:

答案 0 :(得分:0)

让我们不要让事情复杂化,让事情变得更简单。

setInterval(function(){xyz()}, 300000); 

var xyz = function()
{
    //Do what you want your function to do.
}

这里函数xyz将每300,000毫秒调用一次,这意味着5分钟。

如果您要定期使用它来申请,请查看Node Cron btw。

答案 1 :(得分:0)

可能类似以下内容:

// 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);
   }

};

注意:

  1. 用户消息事件在计时器上应用新设置。您可以让bot重复用户消息或自定义内容。
  2. 或者,您只能配置一次计时器。