1

So, I've been looking at this for a while now and haven't been able to understand what's up with it. Most people with problems forget to assign a variable to the setInterval and try to clear the function as oppose to the variable but that isn't the case for me.

My code:

Setting the interval (Works fine):

if (message.author.id === "266315986100027394") {
    if (cmd === prefix && messageArray.length > 1 && messageArray.length < 3
        && messageArray[1] === "DavidJS") {
        var DavidJSTimer = setInterval(DavidJSFunc, 5000);
        function DavidJSFunc() {
            return message.channel.send("Message");
            //console.log("Works");
        }
    }
}

And clearing the interval (Not working):

if(cmd === prefix && messageArray.length > 1 && messageArray[1] === "stopDavidJS") {
    clearInterval(DavidJSTimer);
}

They're both in the same .js file both they have more code between them that is unrelated.

Thanks to anyone who suggests anything.

jo_va
  • 13,504
  • 3
  • 23
  • 47
Cost Death
  • 27
  • 4
  • Are you sure `clearInterval(DavidJSTimer)` is firing? AKA is it making it past the if-statement? I feel like a lot of situations like this are because of something simple like that. – Shelby115 Feb 08 '19 at 13:47
  • define DavidJSTimer globally (only for test) and check again – Alexey Obukhov Feb 08 '19 at 13:47
  • If you'd provide the complete code, probably we could check better the issue. Maybe the `setInterval` reference is not global. Maybe you are not calling correctly the `clearInterval` and so on. – Azametzin Feb 08 '19 at 13:48
  • They not only need to be in the same script, they must be in the same *function*, as that is the scope of the `DavidJSTimer` variable. Quick & dirty solution, declare that variable as a global variable instead of using `var` inside a function. – trincot Feb 08 '19 at 13:49
  • What ***is*** the actual value of `messageArray[1]` in each case? – PM 77-1 Feb 08 '19 at 13:50
  • drop `var`. What happens? – Matt Way Feb 08 '19 at 13:50
  • @Shelby115 it is firing. That was the first thing I tested. – Cost Death Feb 08 '19 at 13:50
  • @CostDeath Try adding `&& DavidJSTimer` to your if-statement and see if it still fires. – Shelby115 Feb 08 '19 at 13:51
  • NB: it makes no sense to have `return` at the end of your interval callback function. The return value is ignored. – trincot Feb 08 '19 at 13:51
  • What does everyone mean a global variable? – Cost Death Feb 08 '19 at 13:51
  • 1
    By global, they mean the *same* variable should be available when you do `clearInterval(DavidJSTimer)`. Are `setInterval` and `clearInterval` code inside the **same `function`** or different functions? Because variables declared with `var` are scoped to the nearest function – adiga Feb 08 '19 at 13:53
  • Read [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript). This question needs a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your problem – Liam Feb 08 '19 at 13:54
  • @Shelby115 once I add that it doesn't work – Cost Death Feb 08 '19 at 13:59
  • @adiga I believe they might be different functions (not sure as they are if statements not function statements) – Cost Death Feb 08 '19 at 13:59
  • @CostDeath That means that when `clearInterval(DavidJSTimer)` is called it's not clearing it because `DavidJSTimer` is undefined. You have a scoping issue. Making it a global variable should work, but ultimately, the call to `clearInterval` has to be in the same scope as the declaration of `DavidJSTimer`. – Shelby115 Feb 08 '19 at 14:02
  • @MattWay on the first it is DavidJS and the second stopDavidJS – Cost Death Feb 08 '19 at 14:02
  • You are not sure. Is there a `function(){}` wrapped outside of your `if (message.author.id === "266315986100027394") {}` code? – adiga Feb 08 '19 at 14:02
  • 1
    Ok figured out what making a function global meant, it worked. Thanks. – Cost Death Feb 08 '19 at 14:05
  • Okay, cool. I have marked it as a duplicate. You can accept it. – adiga Feb 08 '19 at 14:07

0 Answers0