-1

I'm trying to make the setInterval clear when a user types in the off command, how would I go about doing this? Basically, the user types the on command then then turn it off using the off command. This is a snippet of what I currently have.

bot.on('message', (message) => {

    if (message.content.startsWith(prefix + 'on')) {
        var check = setInterval(test, 5000); 

    } else if (message.content.startsWith(prefix + 'off')) {
        clearInterval(test);

    }

    function test() {
    console.log('hello');

    }
}
hsel
  • 163
  • 1
  • 7

1 Answers1

0

change code to

var check=null;
bot.on('message', (message) => {    
    if (message.content.startsWith(prefix + 'on')) {
        check = setInterval(test, 5000);     
    } else if (message.content.startsWith(prefix + 'off')) {
        clearInterval(check);    
    }

    function test() {
        console.log('hello');
    }
}
Behnam
  • 6,244
  • 1
  • 39
  • 36