I'm working on a bot and I want to set state of a button from enable to disabled.
here, I'm confused as if I should use setTimeout() to disable button after 20 seconds or leave it as it is. As setTimeout() is called everytime when user uses "!purchases" command.
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);
After what I've searched on google I've found that setTimeout() works in a async manner which mean if I've process1 as my main discord bots reply, process2, process3 and process4 are setTimeout() then node.js will not execute all these operations in parallel instead it will execute process1 if process2 is fetching or sending data, and vice versa.
Q0) is what I've found incorrect or has information missing?
Q1) So is it recommended in this scenario to use setTimeout()?
Q2) Will setTimeout() compound over the time or not?
Q3 Will there be any perfomence issue due to setTimeout()
CodePudding user response:
I think you found some good information, but you may not fully understand it.
About asynchronous nature of setTimeout() from MDN
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.
So your setTimeout() should not block other functions.
Answering to other questions:
I think that use of setTimeout() here is correct.
The setTimeout should not compound over time - each setTimeout gets a unique timeoutID and it is returned as positive integer value of setTimeout() function.
There should not be performence issues.