2014-10-07 74 views
-1

我有这个倒计时功能,但是如果我想在运行模式下停止这个功能,我该怎么做?这样做将是如何停止倒计时功能然后运行

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

只使用'clearInterval(定时器);'你有当你想要它 – Kyojimaru 2014-10-07 10:05:52

回答

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); 
+0

@Kyborek没有OP在问,我们该如何停止在它的过程中间的区间。 – 2014-10-07 10:11:42

+0

Dosen't工作,我想停止间隔然后运行。 – 2014-10-07 10:19:01

+0

@KevinLund http://jsfiddle.net/b0j6ukkd/看到这个.. – 2014-10-07 10:24:21