我应该在 discord.js 的生产环境中使用 setTimeout() 吗?
Should I use setTimeout() in production with discord.js?
我正在开发一个机器人,我想将按钮的状态从启用设置为禁用。
在这里,我很困惑,好像我应该在 20 秒后使用 setTimeout() 禁用按钮或保持原样。因为每次用户使用“!purchases”命令时都会调用 setTimeout()。
setTimeout(() => {
row.components[0].setDisabled(true);
row.components[1].setDisabled(true);
row.components[2].setDisabled(true);
sentMessage.edit({
content: 'You cannot interact with buttons now!',
components: [row],
});
}, 20000);
在我搜索 google 之后,我发现 setTimeout() 以异步方式工作,这意味着如果我将 process1 作为我的主要 discord 机器人回复,process2、process3 和 process4 是setTimeout() 然后 node.js 不会并行执行所有这些操作,而是如果 process2 正在获取或发送数据,它会执行 process1,反之亦然。
Q0) 是我发现的错误还是缺少信息?
Q1) 那么这种场景推荐使用setTimeout()吗?
Q2) setTimeout()会不会复合时间?
Q3 setTimeout()会不会有性能问题
我认为你找到了一些很好的信息,但你可能没有完全理解它。
关于 MDN
中 setTimeout() 的异步性质
setTimeout() is an asynchronous function, meaning that the timer function will not pause execution of other functions in the functions stack. In other words, you cannot use setTimeout() to create a "pause" before the next function in the function stack fires.
因此您的 setTimeout() 不应阻止其他函数。
回答其他问题:
我认为这里使用setTimeout()是正确的。
setTimeout 不应随时间复合 - 每个 setTimeout 都有一个唯一的 timeoutID,它作为 setTimeout() 函数的正整数值返回。
应该不会有性能问题。
我正在开发一个机器人,我想将按钮的状态从启用设置为禁用。
在这里,我很困惑,好像我应该在 20 秒后使用 setTimeout() 禁用按钮或保持原样。因为每次用户使用“!purchases”命令时都会调用 setTimeout()。
setTimeout(() => {
row.components[0].setDisabled(true);
row.components[1].setDisabled(true);
row.components[2].setDisabled(true);
sentMessage.edit({
content: 'You cannot interact with buttons now!',
components: [row],
});
}, 20000);
在我搜索 google 之后,我发现 setTimeout() 以异步方式工作,这意味着如果我将 process1 作为我的主要 discord 机器人回复,process2、process3 和 process4 是setTimeout() 然后 node.js 不会并行执行所有这些操作,而是如果 process2 正在获取或发送数据,它会执行 process1,反之亦然。
Q0) 是我发现的错误还是缺少信息?
Q1) 那么这种场景推荐使用setTimeout()吗?
Q2) setTimeout()会不会复合时间?
Q3 setTimeout()会不会有性能问题
我认为你找到了一些很好的信息,但你可能没有完全理解它。
关于 MDN
中 setTimeout() 的异步性质setTimeout() is an asynchronous function, meaning that the timer function will not pause execution of other functions in the functions stack. In other words, you cannot use setTimeout() to create a "pause" before the next function in the function stack fires.
因此您的 setTimeout() 不应阻止其他函数。
回答其他问题:
我认为这里使用setTimeout()是正确的。
setTimeout 不应随时间复合 - 每个 setTimeout 都有一个唯一的 timeoutID,它作为 setTimeout() 函数的正整数值返回。
应该不会有性能问题。