停止执行setInterval

时间:2014-11-19 19:17:12

标签: javascript angularjs setinterval

我正在构建一个小幻想游戏。在游戏中,玩家有耐力限制。对于地图上的每次点击,耐力栏都会增加:

angular.element('.click').click(function() {
var endurance_click = angular.element('.progressbar').height();
angular.element('.progressbar').css('height', endurance_click-20);
});

我想要完成的事情是,如果你在地图上点击,并在你的耐力耗尽之前停下来,并等待2秒,我想发射一个恢复你耐力的功能。当你决定继续前进时,我想停止这个功能。

我尝试了setInterval,但功能一直在运行。

这是我的代码:

//Call a function that should restore the endurance
d = window.setInterval(restoreEndurance2, 2000);

function restoreEndurance2()
{
    var endurance_restore = angular.element('.progressbar').height();
    angular.element('.progressbar').css('height', endurance_restore+20);
    if(endurance_restore >= 320) //If it's restored
    {
        alert(endurance_restore);
        clearInterval(d);
    }
}

当我开始浏览地图时,如何让功能停止?

0 个答案:

没有答案