Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/457.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我如何将debounce应用于我的discord机器人?_Javascript_Discord_Discord.js_Bots_Debounce - Fatal编程技术网

Javascript 我如何将debounce应用于我的discord机器人?

Javascript 我如何将debounce应用于我的discord机器人?,javascript,discord,discord.js,bots,debounce,Javascript,Discord,Discord.js,Bots,Debounce,我无法为此邮件添加debounce。我想让机器人先像它一样立即发送消息,然后在再次发送之前暂停7秒以上。我不确定如何或在哪里应用debounce bot.on("typingStart", (channel , user) => { if (channel.id === Join2_channel) { channel.send('Type !join'); }); 您可以在下面划线: 创建并返回传递函数的新取消公告版本,该版本将推迟其执行

我无法为此邮件添加debounce。我想让机器人先像它一样立即发送消息,然后在再次发送之前暂停7秒以上。我不确定如何或在哪里应用debounce

bot.on("typingStart", (channel , user) => {

    if (channel.id === Join2_channel) {
    channel.send('Type !join');
    
});
您可以在下面划线:

创建并返回传递函数的新取消公告版本,该版本将推迟其执行,直到自上次调用该函数以来已过等待毫秒。用于实现只应在输入停止到达后发生的行为。例如:呈现降价注释的预览,在窗口停止调整大小后重新计算布局,等等。 在等待间隔结束时,将使用最近传递给取消公告函数的参数调用函数

将立即参数的值传递为true,以使解Bounce在等待间隔的前缘而不是后缘触发函数。在防止意外双击“提交”按钮第二次触发等情况下非常有用

您也可以直接复制该函数

// When a sequence of calls of the returned function ends, the argument
// function is triggered. The end of a sequence is defined by the `wait`
// parameter. If `immediate` is passed, the argument function will be
// triggered at the beginning of the sequence instead of at the end.
function debounce(func, wait, immediate) {
  var timeout, result;

  var later = function(context, args) {
    timeout = null;
    if (args) result = func.apply(context, args);
  };

  var debounced = restArguments(function(args) {
    if (timeout) clearTimeout(timeout);
    if (immediate) {
      var callNow = !timeout;
      timeout = setTimeout(later, wait);
      if (callNow) result = func.apply(this, args);
    } else {
      timeout = delay(later, wait, this, args);
    }

    return result;
  });

  debounced.cancel = function() {
    clearTimeout(timeout);
    timeout = null;
  };

  return debounced;
}
// Some functions take a variable number of arguments, or a few expected
// arguments at the beginning and then a variable number of values to operate
// on. This helper accumulates all remaining arguments past the function’s
// argument length (or an explicit `startIndex`), into an array that becomes
// the last argument. Similar to ES6’s "rest parameter".
function restArguments(func, startIndex) {
  startIndex = startIndex == null ? func.length - 1 : +startIndex;
  return function() {
    var length = Math.max(arguments.length - startIndex, 0),
        rest = Array(length),
        index = 0;
    for (; index < length; index++) {
      rest[index] = arguments[index + startIndex];
    }
    switch (startIndex) {
      case 0: return func.call(this, rest);
      case 1: return func.call(this, arguments[0], rest);
      case 2: return func.call(this, arguments[0], arguments[1], rest);
    }
    var args = Array(startIndex + 1);
    for (index = 0; index < startIndex; index++) {
      args[index] = arguments[index];
    }
    args[startIndex] = rest;
    return func.apply(this, args);
  };
}
bot.on("typingStart", debounce((channel, user) => {

}, 7000, true));
//当返回函数的一系列调用结束时,参数
//功能被触发。序列的结尾由'wait'定义`
//参数。如果传递了'immediate',则参数函数将为
//在序列的开头而不是结尾触发。
函数解盎司(函数、等待、立即){
var超时,结果;
var later=函数(上下文,参数){
超时=空;
如果(args)结果=函数应用(上下文,args);
};
var debounced=重新辩论(函数(args){
if(超时)cleartimout(超时);
如果(立即){
var callNow=!超时;
超时=设置超时(稍后,等待);
如果(callNow)结果=函数应用(此参数为args);
}否则{
超时=延迟(稍后,wait,this,args);
}
返回结果;
});
debounced.cancel=函数(){
clearTimeout(超时);
超时=空;
};
退场;
}
//有些函数采用可变数量的参数,或采用一些预期参数
//参数,然后是要操作的可变数量的值
//开。此帮助程序将所有剩余参数累加到函数的
//参数长度(或显式的“startIndex”)转换为
//最后一个论点。类似于ES6的“rest参数”。
函数重新辩论(func、startIndex){
startIndex=startIndex==null?函数长度-1:+startIndex;
返回函数(){
var length=Math.max(arguments.length-startIndex,0),
剩余=数组(长度),
指数=0;
对于(;索引<长度;索引++){
rest[index]=参数[index+startIndex];
}
开关(startIndex){
案例0:返回函数调用(this,rest);
案例1:返回函数调用(this,参数[0],rest);
案例2:返回函数调用(this,参数[0],参数[1],rest);
}
var args=数组(startIndex+1);
对于(索引=0;索引{
},7000,对);

只需跟踪您上次发送邮件的时间(
new Date();
),在发送另一封邮件之前,请确保至少已经过了7秒then@joeydibbs这种方法对你有用吗?当用这种方法更新我的代码时,我得到的错误没有定义。bot.on(“typingStart”、u.debounce((channel,user)=>{if(channel.id==Join2_channel){channel.send('Type!join');},7000,true));@joyedibbs你在使用下划线.js吗?你的标题似乎表明了这一点。我不是。对不起,我可以链接到类似jquery的库吗?@joyedibbs是的,你可以这样添加:
// When a sequence of calls of the returned function ends, the argument
// function is triggered. The end of a sequence is defined by the `wait`
// parameter. If `immediate` is passed, the argument function will be
// triggered at the beginning of the sequence instead of at the end.
function debounce(func, wait, immediate) {
  var timeout, result;

  var later = function(context, args) {
    timeout = null;
    if (args) result = func.apply(context, args);
  };

  var debounced = restArguments(function(args) {
    if (timeout) clearTimeout(timeout);
    if (immediate) {
      var callNow = !timeout;
      timeout = setTimeout(later, wait);
      if (callNow) result = func.apply(this, args);
    } else {
      timeout = delay(later, wait, this, args);
    }

    return result;
  });

  debounced.cancel = function() {
    clearTimeout(timeout);
    timeout = null;
  };

  return debounced;
}
// Some functions take a variable number of arguments, or a few expected
// arguments at the beginning and then a variable number of values to operate
// on. This helper accumulates all remaining arguments past the function’s
// argument length (or an explicit `startIndex`), into an array that becomes
// the last argument. Similar to ES6’s "rest parameter".
function restArguments(func, startIndex) {
  startIndex = startIndex == null ? func.length - 1 : +startIndex;
  return function() {
    var length = Math.max(arguments.length - startIndex, 0),
        rest = Array(length),
        index = 0;
    for (; index < length; index++) {
      rest[index] = arguments[index + startIndex];
    }
    switch (startIndex) {
      case 0: return func.call(this, rest);
      case 1: return func.call(this, arguments[0], rest);
      case 2: return func.call(this, arguments[0], arguments[1], rest);
    }
    var args = Array(startIndex + 1);
    for (index = 0; index < startIndex; index++) {
      args[index] = arguments[index];
    }
    args[startIndex] = rest;
    return func.apply(this, args);
  };
}
bot.on("typingStart", debounce((channel, user) => {

}, 7000, true));