2014-08-28 160 views
0
function start() { 
    rM = Math.floor((Math.random() * 1) + 1); 
    rS = Math.floor((Math.random() * 59) + 1); 
    $('#minutes').text(rM); 
    $('#seconds').text(rS); 

int1 = setInterval(function timer() { 
    if(rM > 0 && rS > 0) 
    { 
     rS--; 
     $('#seconds').text(rS); 
    } 

    if(rM > 0 && rS == 0) 
    { 
     rM--; 
     rS = 3; 
     $('#minutes').text(rM); 
     $('#seconds').text(rS); 
    } 

    if(rM == 0 && rS > 0) 
    { 
     rS--; 
     $('#seconds').text(rS); 
    } 

    if(rM == 0 && rS == 0) 
    { 
     $('#minutes').text(rM); 
     $('#seconds').text(rS); 

     function timerChange() { 
      if(myGallery.imgIndex != myGallery.indexMax) 
      { 
       myGallery.imgIndex++; 
       changeNext(); 
       btnNext.disabled = false; 

       rM = Math.floor((Math.random() * 1) + 1); 
       rS = Math.floor((Math.random() * 59) + 1); 
       $('#minutes').text(rM); 
       $('#seconds').text(rS); 
      } 

      else 
      { 
       myGallery.imgIndex = 1; 
       changeNext(); 
       btnNext.disabled = false; 

       rM = Math.floor((Math.random() * 1) + 1); 
       rS = Math.floor((Math.random() * 59) + 1); 
       $('#minutes').text(rM); 
       $('#seconds').text(rS); 
      } 
     } 

     timerChange(); 

    } 


}, 1000); 


}; 

    $('#pause').click(function() 
    { 
     if(paused == false) 
     { 

      clearInterval(int1), 
      paused = true; 
     } 

     else 
     { 

      //some code to start timer again 
      paused = false; 
     } 
    }); 

start(); 

我有一些定时器功能,以分,秒,我的问题是从哪里是被“clearInterval”或其他任何方式ü可以提供停下的地方运行计时器.. Proly我需要用rM和rS的值再次运行'start'函数,这一刻我停止了它,但我不知道如何。JQuery的,开始停止计时器

回答

0

当您恢复时,不要设置rM和rS。

 function start(resume) { 
      if (!resume) { 
       rM = Math.floor((Math.random() * 1) + 1); 
       rS = Math.floor((Math.random() * 59) + 1); 
      } 
     ... 


     $('#pause').click(function() { 
      if (paused == false) { 
       clearInterval(int1); 
       paused = true; 
      } 
      else { 
       //some code to start timer again 
       paused = false; 
       start(true); 
      } 
     }); 

见琴:

http://jsfiddle.net/b2ppnw7x/

+0

工作良好,THX很多) – 2014-08-28 14:39:56