javascript - 如何停止函数的执行?

标签 javascript node.js discord.js

所以我基本上想为我的不和谐票证机器人创建一个倒计时。如果有人输入 -close[...], channel 将在 10 秒后被删除。但是,如果执行此命令的人在 channel 中键入某些内容,倒计时将停止并且 channel 不会被删除。

到目前为止效果很好。但是,如果我中止发送到 channel 的所有其他消息的倒计时,嵌入将被发送到显示“倒计时已停止”的位置,如果我再次输入 -close [...],则会弹出此消息,但 channel 仍将是10 秒后删除。

function closeTicket (_ticketid, channel, deleter, reason) {
    var timer = setTimeout(function() {
      channel.delete();
    }, 10000);
    channel.send({embed: {
      color: 3447003,
      author: {
        name: client.user.username,
        icon_url: client.user.avatarURL
      },
      title: ``,
      description: "This ticket will close in 10 seconds. If this was a mistake type anything to stop the timer.",
      fields: [{
          name: "Thank you!",
          value: "Thank you for using our ticket system! Good luck and have fun playing on our servers."
        },
      ],
      timestamp: new Date(),
      footer: {
        icon_url: client.user.avatarURL,
        text: "Support Ticket System © H4rry#6701"
      }
    }
    });
    logTicketClosed(_ticketid, deleter, reason);
    client.on('message', message => {
      if(message.channel === channel && message.author === deleter && timer != null) {
        clearTimeout(timer);
        timer = null;
        message.channel.send({embed: {
          color: 3447003,
          author: {
            name: client.user.username,
            icon_url: client.user.avatarURL
          },
          title: `Timer Stopped`,
          description: "The timer has been stopped, the ticket will remain open.",
          timestamp: new Date(),
          footer: {
            icon_url: client.user.avatarURL,
            text: "Support Ticket System © H4rry#6701"
          }
        }});
      }
    });
    return 0;
  }

最佳答案

我现在可以使用它了!我定义了一个名为 timer_running 的新变量,在计时器启动时将其设置为 true,在计时器停止时设置为 false。这样我现在就可以工作了。

  function closeTicket (_ticketid, channel, deleter, reason) {
    var timer_running = false;
    var timer = setTimeout(function() {
      channel.delete();
    }, 10000);
    timer_running = true;
    channel.send({embed: {
      color: 3447003,
      author: {
        name: client.user.username,
        icon_url: client.user.avatarURL
      },
      title: ``,
      description: "This ticket will close in 10 seconds. If this was a mistake type anything to stop the timer.",
      fields: [{
          name: "Thank you!",
          value: "Thank you for using our ticket system! Good luck and have fun playing on our servers."
        },
      ],
      timestamp: new Date(),
      footer: {
        icon_url: client.user.avatarURL,
        text: "Support Ticket System © H4rry#6701"
      }
    }
    });
    logTicketClosed(_ticketid, deleter, reason);
    client.on('message', message => {
      if(message.channel === channel && message.author === deleter && timer_running === true) {
        clearTimeout(timer);
        timer_running = false;
        message.channel.send({embed: {
          color: 3447003,
          author: {
            name: client.user.username,
            icon_url: client.user.avatarURL
          },
          title: `Timer Stopped`,
          description: "The timer has been stopped, the ticket will remain open.",
          timestamp: new Date(),
          footer: {
            icon_url: client.user.avatarURL,
            text: "Support Ticket System © H4rry#6701"
          }
        }});
      }
    });
  }

关于javascript - 如何停止函数的执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57117598/

相关文章:

node.js - PostgreSQL 的 Sequelize.js 事务问题

javascript - 如何停止在 MySQL 中舍入 int 值

javascript - 使用 jQuery 禁用选择时的表单字段

javascript - 我如何使用 javascript 文件夹对象

javascript - 在类范围之外触发​​事件 javascript

node.js - 显示断言失败的预期值和实际值

javascript - Discord Bot 设置某人的昵称

javascript - 嵌入的一个值中的多行(discord.js)

javascript - 如何读取本地pdf文件并将其作为expressJS应用程序的响应发送?

javascript - 并行化 webpack 构建以提高性能?