如何停止倒计时功能然后运行

时间:2014-10-07 10:03:18

标签: jquery

我有这个倒计时功能,但如果我想在运行模式下停止此功能,我该怎么做?

function GameCounter(){

        var sec = 6
        $("#gamecounter span").html(sec)

        var timer = setInterval(function() { 
            $('#gamecounter span').text(sec--);
            if (sec == -1) {
               newcards();
                    clearInterval(timer);
            } 
        }, 1000);

 }

1 个答案:

答案 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);