-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);
}
但计时器不断进入负数字。请帮助