-2
Попытка создать простой обратный отсчет, но я хочу, чтобы он остановился, как только таймер достигнет 0 .. Я не знаю, как его реализовать.Count down stop at 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 (scope.workTimer === 0) {
$interval.cancel(timeSet);
}
но таймер продолжает идти в отрицательные цифры. Пожалуйста, помогите