I have a variable of unknown value, it will be an integer. For this sake, lets say var a = 3;
I have a function that is called continuously:
<
Here's an approach that encapsulates the counter, but uses a global variable for "a":
var a = 3;
function anim(){
// Run the usual code here
// ...
if (++anim.counter % a === 0) {
// Run the special code here
// ...
}
}
// Initialize static properties.
anim.counter = 0;
And here's an approach that encapsulates the "a" variable as well, referring to it as "frequency":
function anim(){
// Run the usual code here
// ...
if (++anim.counter % anim.frequency === 0) {
// Run the special code here
// ...
}
}
// Initialize static properties.
anim.counter = 0;
anim.frequency = 1;
Then set the desired frequency value before calling anim() for the first time:
anim.frequency = 3;