0

I am trying to make a bot clear an interval if my config file says "off" but leave it running if it says "on".

I've already tried doing

discord.js

    config.Interval = setInterval(() => {
        WallCheck.send(WallCheckembed);
    }, 500);

    clearInterval(off)
};

but when I put in the "config.Interval" its setting the setInterval to "config.Interval".

discord.js

    config.Interval = setInterval(() => {
        WallCheck.send(WallCheckembed);
    }, 500);

This is my code. And my config file is:

"Interval": "off"

The only error I'm getting is this:

(node:6844) UnhandledPromiseRejectionWarning: ReferenceError: off is not defined

And the variable "off" is referring to the "off" in my clear interval.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Kraken
  • 51
  • 5
  • 1
    `And the variable "off" is referring to the "off" in my clear interval.` You don't have an `off` defined. – Matt Aft Oct 21 '19 at 05:17

1 Answers1

0

Welcome to stackoverflow ! When clearing an interval, you need to pass the variable created by the setInterval.:

var count = 0;
var myInterval = setInterval(()=> {
    // i do somehting every 5 second
    if(count >= 10) {
        // we can access variable outside the callback because we are using the arrow operator.
        clearInterval(myInterval)
    }
    count++;
}, 5000);
Nicolas
  • 8,077
  • 4
  • 21
  • 51