N
N
not important .2021-05-22 13:04:39
JavaScript
not important ., 2021-05-22 13:04:39

How can I make it run only 1 interval?

I want the bot to write "done" to the chat every 10 seconds after a certain command, but if I write the command 2 times, then after 10 seconds the bot writes "done" 2 times and it turns out that 2 intervals are running, but I only need 1.

Example:

bot.on('message', async message => {
function log {
client.channels.cache.get("id_channel").send("done")
};
let interval = setInterval(log, 10000);
};

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-05-22
@26DiDi12

Move interval from a function local variable to somewhere where it will be stored for a long time.
For example, in a global variable, or better in a class field.
Then you can check if interval is not undefined, then the timer has already been set.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question