2017-01-08 7 views
-2

単純なカウントダウンを作成しようとしていますが、私はタイマーが0に達するとそれを停止する。私はそれを実装する方法はわかりません。カウントダウン0でカウントダウン

function myButton($interval) { 
    return { 
     templateUrl: '/templates/myButton.html', 
     restrict: 'E', 
     replace: true, 
     scope: { }, 
     link: function(scope, element, attributes) { 
      scope.workTime = 3; 
      scope.buttonText = "Start"; 

      var timeSet; 

      scope.countdown = function() { 
       scope.workTime--; 
      } 
      scope.startTimer = function() { 
       if(scope.buttonText == "Reset") { 
        scope.workTime = 3; 
        $interval.cancel(timeSet); 
        scope.buttonText = "Start"; 
        console.log("test restarted"); 

       } else { 
        timeSet = $interval(scope.countdown,1000); 
        scope.buttonText = "Reset"; 
        console.log("test started"); 
       } 
      } 
     } 
    } 

}; 


<div> 
    <h1>{{ workTime}}</h1> 
    <button ng-click="startTimer()">{{buttonText}}</button> 
<div> 

if文私が試してみた:

if (scope.workTimer === 0) { 
$interval.cancel(timeSet); 
} 

をしかし、タイマーは、負の数字にいっています。

scope.countdown = function() { 

     if(scope.workTime === 0) 
     { 
     $interval.cancel(timeSet); 
     } 
     else 
     { 
     scope.workTime--; 
     } 
    } 

scope.countdownにあなたは0値の条件を確認する必要があり

答えて