setInterval and clearInterval doesn't work

I don’t understand why setInterval and clearInterval doesn’t work…

//On to starting my commmand
//Off to stopping my command

const { MESSAGES } = require('../../Data/constants')

module.exports.run = (client, message, args) => {

    switch (args[0]) {

        case 'on': {

            var interval = setInterval (function() {
                
                console.log('coucou')}, 2000);
        };
 
        case 'off': {

            clearInterval(interval);
        };

        break;
    };
};

module.exports.help = MESSAGES.COMMANDS.INFO.SPAM

4 thoughts on “setInterval and clearInterval doesn't work”

  1. I don’t understand why setInterval and clearInterval doesn’t work…

    The first step to understanding why your code doesn’t work is getting the basic syntax and format right…

    The code you’ve provided has several extremely basic issues with it. I would suggest learning quite a bit more javascript before working on creating a full discord bot, or you will face issue after issue literally every step of the way, for every single command you write that does anything more complicated than printing a message. It’s always beneficial to have a more advanced knowledge of a programming language before attempting to make advanced projects with it like a discord bot.

    Your switch statement syntax is immensely incorrect. This is what your switch looks like:

    switch (args[0]) {
    
        case 'on': {
            //On Code
        };
    
        case 'off': {
            //Off Code
        };
    
        break;
    };
    

    This is what it is supposed to look like:

    switch (args[0]) {
    
        case 'on':
            //On Code
            break;
    
        case 'off':
            //Off Code
            break;
    
    };
    

    You need to write the code in the proper way in order for it to work. Not to mention, you didn’t add a break; to your ‘on’ case, meaning both your on and off commands were trying to clear the interval, and never created an interval in the first place. With a single google search, you could’ve found the proper syntax for a switch statement, compared it with your own code, and fixed your code yourself. Try doing some research next time, before asking a question here. You need to actually try to fix the problem you are experiencing on your own before asking a question on StackOverflow, we are here to help you on issues you are actually stuck on, not to write parts of your bot for you.

    And that’s not the only issue in your code. Even after correcting the switch syntax, look at your code:

    switch (args[0]) {
    
        case 'on':
            var interval = setInterval (function() {
                console.log('coucou');
            }, 2000);
            break;
    
        case 'off':
            clearInterval(interval);
            break;
    
    };
    

    The variable interval is defined in the ‘on’ case. That means the interval variable only exists within the ‘on’ case. In the ‘off’ case, no variable called interval exists, you are trying to clear a non-existent interval. You need to define the interval variable outside of the entire run() method, probably near the top of the file, and then use it to set and clear the interval from there. I will not post the actual full code for doing so in this answer because it is simple to do from what I just described, and I want to avoid the situation in which the answer is simply copied and pasted instead of any effort to really understand what the issue is. You will need to modify the above block of code yourself, in the way that I described, in order to make this code work.

    Reply

Leave a Comment