I want to have a single button that acts as a start and stop button for some recurring time events.
To do this, I can have a global variable at the top of everything:
toggleOn = false;
And then, inside <button onClick="..., I can have:
toggleOn =! toggleOn;
foo();
function foo() {
if (toggleOn) {
setTimeout(foo, 5000);
}
}
But the problem is that I should not use a global variable to perform the same task. How am I supposed to do this? Is there a persist variable that can carry a value outside its scope?
source
share