client.on('message', msg => {
if (msg.content === 'startspam') {
function spam(){
msg.channel.send('a')
}
setInterval(spam, 2500)
}
});
client.on('message', msg => {
if (msg.content === 'stopspam') {
}
});
how to stop the function spam ( sorry I'm new to JavaScript and im trying to learn the basics )
You can use the clearInterval function on the number that setInterval returns to stop the interval. I added some code below to show how that works.
var interval = setInterval(someFuntion, 200);
clearInterval(interval);
Try clearInterval
const print = () => console.log('alive');
const intervalId = setInterval(print, 1000);
const stopInterval = () => clearInterval(intervalId);
<button onclick='stopInterval()'>Stop</button>
first of all java and javascript are not the same..
and for your question -
you can use this clearInterval().
this function receives the id of the interval you want to stop. and the interval Function returns a unique number so it can used as ID..
the way you do it is somthing like this :
var intervalID = '';
function startInterVal() {
intervalID = setInterval(() => {
console.log('Hello')
}, 1000)
}
function stopInterval() {
clearInterval(intervalID);
}
<button onClick="startInterVal()">Start</button>
<button onClick="stopInterval()">Stop</button>
hope i helped you :)