2017-02-09 2 views
0

나는 변수에 따라 카운트 다운 시계를 표시하는 기능이 있습니다. 특정 조건에서 그것을 멈추게하는 올바른 방법은 무엇입니까? clearTimeout은 감소 함수를 중지하지 않습니다.상태가 자바 스크립트 카운트 다운 함수를 중지

function interface_vip(type){ 
    var timeout = ''; 
    var clock = ''; 
    //display clock 
    function Decrement() { 
     currentMinutes = Math.floor(secs/60); 
     currentSeconds = secs % 60; 
     if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds; 
      secs--; 
      if(secs !== -1) timeout = setTimeout(Decrement,1000); 
    } 
    if (type == 1){ 
     var mins = 20; 
     var secs = mins * 60; 
     var currentSeconds = 0; 
     var currentMinutes = 0; 
     clock = setTimeout(Decrement,1000); 
    } 
    if (type == 2){ 
     clearTimeout(clock); 
     clearTimeout(timeout); 
    } 
} 
+0

오류가 있습니다. t는 정의되지 않았습니다. –

+1

l ook는'interface'가 JavaScript의 예약어입니다 https://mathiasbynens.be/notes/reserved-keywords#ecmascript-2 –

+0

변수 t가 제 js에 정의되어 있습니다. 제 질문에 그것을 숫자로 바꿀 것입니다. 작동 – Adry

답변

2

귀하의 클럭 ID가 두 번째 통화에서 손실, 가난한 솔루션은

var timeout = ''; 
     var clock = ''; 
    function interface_vip(type){ 
     //display clock 
     function Decrement() { 
      currentMinutes = Math.floor(secs/60); 
      currentSeconds = secs % 60; 
      if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds; 
       secs--; 
       if(secs !== -1) timeout = setTimeout(Decrement,1000); 
     } 
     if (type == 1){ 
      var mins = 20; 
      var secs = mins * 60; 
      var currentSeconds = 0; 
      var currentMinutes = 0; 
      clock = setTimeout(Decrement,1000); 
     } 
     if (type == 2){ 
      clearTimeout(clock); 
      clearTimeout(timeout); 
     } 
    } 

벨로우즈 조각

function interface_vip(){ 
 
    var timeout = ''; 
 
    var t = 0; 
 
    var clock = ''; 
 
    //display clock 
 
    function Decrement() { 
 
     currentMinutes = Math.floor(secs/60); 
 
     currentSeconds = secs % 60; 
 
     if(currentSeconds <= 9) currentSeconds = "0" + currentSeconds; 
 
      secs--; 
 
      if(secs !== -1) timeout = setTimeout(Decrement,1000); 
 
    } 
 
    this.start = function(){ 
 
     var mins = t; 
 
     var secs = mins * 60; 
 
     var currentSeconds = 0; 
 
     var currentMinutes = 0; 
 
     clock = setTimeout(Decrement,1000); 
 
    } 
 
    this.stop = function(){ 
 
     clearTimeout(clock); 
 
     clearTimeout(timeout); 
 
    } 
 
} 
 
var interf = new interface_vip(); 
 
interf.start(); 
 
interf.stop();

에 더 나은 방법 글로벌 변수를 만드는 것입니다
+0

감사합니다! 나는 그것을 볼 수 없었다. 그게 문제를 해결합니다 : D – Adry

+0

답변에 더 나은 해결책을 추가했습니다. –

관련 문제