+ 1
how to stop an interval from another function?
here's a quick info about my code: var rn generates a random number (1-10), if the number is below 2, var stealer will be set to true and will start the interval called stealTimer (steals 2 points per second, located at line 51). i need to stop the interval using the function bribe() (located at line 60) if the conditions are met. the problem is: (at line 69) the console will display an error saying that stealTimer is not defined.. https://code.sololearn.com/WpySlJRLfGhY/?ref=app
1 Answer
+ 4
The reason you're getting that error is because youre defining stealTimer within a local scope, if you change it to a global variable and have it set just within the function it will work, so that both functions can access it.
Reading up on scoping and execution context will help with this. It is very different in javascript than a lot of other languages.