这可能是一个非常新手的问题,但是我有一个带有计时器的游戏,该计时器会逐渐耗尽,当它耗尽时,我会在GAME OVER页面上显示得分。
当分数的显示链接到计时器时,它将持续每秒显示分数。我将如何停止这样做?
var doUpdate = function() {
$('.countdown').each(function() {
var count = parseInt($(this).html());
if (count !== 0) {
$(this).html(count - 1);
} else {
$('.gameover h4').append( "YOU SCORED "+score+"!!!" );
$('.gameover').show();
}
});
};
// Schedule the update to happen once every second
setInterval(doUpdate, 1000);
有没有什么方法可以让侦听器检查计数,所以我不必在setInterval中添加append函数,也可以不用else函数破坏setinterval计时器
谢谢
这是一个演示(可能)http://thetally.efinancialnews.com/tallyassets/wackamouse/index2.html
最佳答案
您需要这样做clearInterval
。
var timeoutev ;
var doUpdate = function() {
$('.countdown').each(function() {
var count = parseInt($(this).html());
if (count !== 0) {
$(this).html(count - 1);
} else {
$('.gameover h4').append( "YOU SCORED "+score+"!!!" );
$('.gameover').show();
clearInterval(timeoutev);
}
});
};
// Schedule the update to happen once every second
timeoutev = setInterval(doUpdate, 1000);
关于jquery - 倒计时结束后显示我的分数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26298575/