最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Incrementing a local variable using JavaScript - Stack Overflow

programmeradmin4浏览0评论

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();
    }
}

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();
    }
}
Share Improve this question edited May 16, 2011 at 16:23 user699242 asked May 16, 2011 at 16:22 user699242user699242 1,8334 gold badges21 silver badges31 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 10
// 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.

It is possible, you can create some object and use the function as its method. That way the variable will be object's property and not the global variable.

If you do not use any framework currently, using MooTools may help you a lot. Some examples for creating and using objects are located here: http://mootools/docs/core/Class/Class

You can try returning what you have (so you can use it later if you want)

function refreshMediaAds(mediaClickCounter) {
    if (mediaClickCounter < 2) {
        mediaClickCounter++;
    } else {
        mediaClickCounter = 0;
        refreshAds();
    }
    return mediaClickCounter;
}
//then do: 
refreshMediaAds(clicks)
function refreshMediaAds() {
var mediaClickCounter = 0; // now variable have local scope.
    if (mediaClickCounter < 2) {
        mediaClickCounter++;
    } else {
        mediaClickCounter = 0;
        refreshAds();
    }
}

If you really must, I believe something like this would do the trick. However, IMO you should just leave it in the global namespace.

var refreshMediaAds = function() {
    var mediaClickCounter = 0;
    return function()
    {
        if (mediaClickCounter < 2) {
            mediaClickCounter++;
        } else {
            mediaClickCounter = 0;
            refreshAds();
        }
    }

}();

Note, I haven't tested this, it's an educated guess at a solution.

发布评论

评论列表(0)

  1. 暂无评论