我有这个倒计时功能,但如果我想在运行模式下停止此功能,我该怎么做?
function GameCounter(){
var sec = 6
$("#gamecounter span").html(sec)
var timer = setInterval(function() {
$('#gamecounter span').text(sec--);
if (sec == -1) {
newcards();
clearInterval(timer);
}
}, 1000);
}
答案 0 :(得分:2)
这样做的一种方法是,
function GameCounter(){
var sec = 6
$("#gamecounter span").html(sec)
var timer = setInterval(function() {
$('#gamecounter span').text(sec--);
if (sec == -1) {
newcards();
clearInterval(timer);
}
}, 1000);
return timer;
}
//In the place where you calling the GameCounter() function.
var timer = GameCounter();
//Call the below line to stop the interval at any point of time.
clearInterval(timer);