I want to have one button, functioning as both the "start" and "stop" button for some reoccurring timed events.
To do this, I can have a global variable on the top of everything:
toggleOn = false;
And then, inside of <button onClick="...
, I can have:
toggleOn =! toggleOn;
foo();
function foo() {
// do my stuff
if (toggleOn) {
setTimeout(foo, 5000);
}
}
But the problem is, I must not use a global variable to complete the same task. How should I do it? Is there a persist variable that can carry a value outside its scope?