I am trying to increment a counter, but I would like to move the variable out of the global namespace and declare it locally. I'm not sure how to do this. Thanks for your help.
This is the code I'm using at the moment.
var mediaClickCounter = 0;
function refreshMediaAds() {
if (mediaClickCounter < 2) {
mediaClickCounter++;
} else {
mediaClickCounter = 0;
refreshAds();
}
}
// the function creates a local scope.
var refreshMediaAds = (function() {
var mediaClickCounter = 0;
// once executed it returns your actual function.
return function _refreshMediaAds() {
if (mediaClickCounter < 2) {
mediaClickCounter++;
} else {
mediaClickCounter = 0;
refreshAds();
}
}
// you execute the function.
})();
Closures <3.
how about using something like this:
var ClickCounter = {
mediaClickCounter: 0,
refreshMediaAds:function() {
if (this.mediaClickCounter < 2) {
this.mediaClickCounter++;
} else {
this.mediaClickCounter = 0;
refreshAds();
}
}
};
and then you can just use ClickCounter.refreshMediaAds() which will increment the member variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With