'How to make a person stop a executing command who started the execution of another command discord.js

I am making a spam command, i want the spam command executer to only be able to stop it. I am confused to how do so. Here's my code (i have tried nothing by by myself yet) else if (command === prefix + "spam") {spamText = setInterval(() => {message.channel.send(${args.slice(0).join(" ")})}, 1500);} else if (command === prefix + "stopspam"{ clearInterval(spamText); message.reply("Stopped the spamming. Hope it created some chaos :skull:")}



Solution 1:[1]

you need to extract your interval to be able to stop it

function Spam(){
    message.channel.send(${args.slice(0).join(" ")})
}
let myInterval;
if (command === prefix + "spam") {
    myInterval = setInterval(Spam, 1500);   
} 
else if (command === prefix + "stopspam"){ 
    clearInterval(spamText); 
    message.reply("Stopped the spamming. Hope it created some chaos :skull:");
}

and be carefull with the use. nobody like spammer

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gautier Logeot