0

I've been wanting to add a feature to my script, which can enable me to stop the setInterval function through a simple command, but the real problem is that I do not know how to do it.

client.on('message', async (message) => {
 if (message.author.username === 'username')
  if (message.content === '*shiba inu pupper') {
   const attachment = new Discord.MessageAttachment('shiba doggo');

   setInterval(() => {
    message.channel.send(attachment);
   }, 20000);
  }
});
Lioness100
  • 8,260
  • 6
  • 18
  • 49

2 Answers2

3

Ex: Your interval function:

let intervalFunc = setInterval(() => {
   // code .....
}, 20000);

After that When you want to stop the interval time, use the below function:

clearInterval(intervalFunc);

Above function will clear your interval time

Tejas Savaliya
  • 572
  • 7
  • 8
2

add my_interval global variable in your js file :

var my_interval;

then:

client.on('message', async (message) => {
 if (message.author.username === 'username')
  if (message.content === '*shiba inu pupper') {
   const attachment = new Discord.MessageAttachment('shiba doggo');

   my_interval = setInterval(() => {
    message.channel.send(attachment);
   }, 20000);
  }
});

and whenever you went to stop that interval just call:

clearInterval(my_interval);
Lioness100
  • 8,260
  • 6
  • 18
  • 49
aziz k'h
  • 775
  • 6
  • 11