2013-11-15 2 views
0

코드는 다음과 같습니다재설정을하여 setInterval은 JS에

function startTimer(counter) { 
    var interval = setInterval(function() { 
     counter--; 
     $('#timer').html(counter); 
     // Display 'counter' wherever you want to display it. 
     if (counter == 0) { 
      clearInterval(interval); 
      $('#question').html("Time ended"); 
      setTimeout(function() { 
       window.location.href = "/"; 
      }, 5000); 
      return false; 
     } 
    }, 1000); 
} 

내가하고 싶은 것은 내가이 함수를 여러 번 호출 할 때마다 30 초 타이머를 재설정하고 과거의 모든 인스턴스를 죽일 수있다. 현재 여러번 전화를 걸면 과거의 거리로 어지럽게 울립니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

+0

@naomik 죄송합니다, 무엇을? – heron

+0

이 링크 참조 : http://stackoverflow.com/questions/8126466/javascript-reset-setinterval-back-to-0 – archu

답변

0

당신은 함수 외부 var에 간격을 정의 할 수 있습니다

var interval; 
    function startTimer(counter) { 
     interval = setInterval(function() { 
      counter--; 
      $('#timer').html(counter); 
      // Display 'counter' wherever you want to display it. 
      if (counter == 0) { 
       clearInterval(interval); 
       $('#question').html("Time ended"); 
       setTimeout(function() { 
        window.location.href = "/"; 
       }, 5000); 
       return false; 
      } 
     }, 1000); 
    } 
관련 문제