2017-10-05 1 views
0

시작 시간이 다른 웹 페이지에 두 개의 타이머를 표시하려고합니다.Angular.js에 여러 타이머 기능 표시

첫 번째 타이머는 5 초 동안 만 표시하고 10 초 후에는 timer2를 표시해야합니다. 저는 Angular에 매우 익숙하며 다음 코드를 작성했습니다.

settimeout이 세 번째로 호출 될 때를 제외하고는 제대로 작동하지 않는 것 같습니다. 세 번째로 올바르게 작동하지 않으며 매우 빠르게 시작됩니다.

컨트롤러 내보기에서

// initialise variables 
$scope.tickInterval = 1000; //ms 
var min =''; 
var sec =''; 

$scope.ti = 0; 
$scope.startTimer1 = function() { 
    $scope.ti++; 
    min = (Math.floor($scope.ti/60)<10)?("0" + Math.floor($scope.ti/60)):(Math.floor($scope.ti/60)); 
    sec = $scope.ti%60<10?("0" + $scope.ti%60):($scope.ti%60); 
    $scope.timer1 = min + ":" + sec; 
    mytimeout1 = $timeout($scope.startTimer1, $scope.tickInterval); // reset the timer 
} 

//start timer 1 
$scope.startTimer1(); 

$scope.$watch('timer1',function(){ 

    if($scope.timer1 !=undefined){ 
     if($scope.timer1 =='00:05'){ 
      $timeout.cancel(mytimeout1); 
      setInterval(function(){ 

       $scope.startTimer2() 
       $scope.ti = 0; 

      },1000) 
     } 
    } 

}) 

//start timer 2 after 2 mins and 20 seconds 
$scope.startTimer2 = function() { 
    $scope.ti++; 
    min = (Math.floor($scope.ti/60)<10)?("0" + Math.floor($scope.ti/60)):(Math.floor($scope.ti/60)); 
    sec = $scope.ti%60<10?("0" + $scope.ti%60):($scope.ti%60); 
    $scope.timer2 = min + ":" + sec; 
    mytimeout2 = $timeout($scope.startTimer2, $scope.tickInterval); 
} 


$scope.$watch('timer2',function(){ 

    if($scope.timer2 !=undefined){ 
     if($scope.timer2 =='00:05'){ 

      $timeout.cancel(mytimeout2); 

      setInterval(function(){ 

       $scope.startTimer1(); 
       $scope.ti = 0; 

      },1000) 


     } 
    } 

}) 

을 나는 단순히 합산 그래서 당신은 기본적으로 여러가 startTimer 기능을 시작하고

<p>{{timer1}}</p> 

<p>{{timer2}}</p> 
+0

당신은 setInterval을하지만, $ 간격을 사용하지 말아야 사용 : 당신은 $ 간격이 방법을 사용할 수 있습니다. – yeouuu

답변

1

있습니다. 내가 당신의 문제를 잘 이해한다면 당신은 모든 관찰자와 시간 초과를 가질 필요조차 없습니다.

$scope.Timer = $interval(function() { 

    ++$scope.tickCount 

    if ($scope.tickCount <= 5) { 
    $scope.timer1++ 
    } else { 
    $scope.timer2++ 
    if ($scope.tickCount >= 10) 
     $scope.tickCount = 0; 
    } 
}, 1000); 

fiddle

+0

고맙습니다.하지만 두 번째 "카운트 업"타이머를 분 단위로 표시해야합니다. 초 형식입니다. 첫 번째 타이머가 02:00에 도달하면 중지/일시 정지가 필요하고 두 번째 타이머는 00 : 01부터 시작해야합니다. 두 번째 타이머가 02:00에 도달하면 일시 중지 한 다음 첫 번째 타이머가 다시 재생됩니다. 그래서 저는 시계를 사용하고있었습니다. – snowflakes74

+0

새로운 피들을 확인하십시오, 당신이 찾고있는 것이어야합니다 - 타이머를 형성하지 않았 음을 유의하십시오. 정수로 코드 샘플을 사용하여 mm로 포맷 할 수 있습니다 : ss –

+1

감사합니다. 효율적으로 내 문제를 해결할 수 있습니다. – snowflakes74