I am programming a simple game using the HTML canvas and editing it with JavaScript. I several functions which draw something on the canvas, and all those functions are looping using a single setInterval. However some of these functions need to have a local variable that contains a certain attribute about that function (to be precise, a toggle, whether a function is "xx" or not), and as there will be many of these functions, storing this variable in the global window is very unpractical.
Is there a way of defining the variable (or attribute) in this function without this variable being reset every loop?
So in code I have:
var DoSomething = function(){
var xx = new Boolean
[...]
if (condition) { xx = false}
if (condition) { xx = true}
}
And all is executed in the main loop:
var gameLoop = function(){
DoSomething()
OtherFunctions()
}
var startGame = function(){
setInterval(gameLoop,10)
}
startGame()
Like I said, defining xx
in the global window would be very unpractical as there are many
function with this xx
attribute/variable. But right now, the loop keeps resetting xx
at the start of the loop. Is there a way of defining the local variable here, without it being reset with every loop of the setInterval?