gpt4 book ai didi

javascript - 如何将计时器值格式更改为 MM :Sec from fixed counter in angular js

转载 作者:行者123 更新时间:2023-12-03 06:07:51 25 4
gpt4 key购买 nike

我有一个计时器的 AngularJS 代码,在启动计数器时,计时器从 300 开始倒计时到 0。它工作正常。但现在我想用 MM:SEC 格式(时钟)替换 300,即 5:00 并继续并在 0:00 结束,但我无法做到。我的代码

    angular.module('TimerApp', [])
.controller('TimerCtrl', function($scope, $timeout) {
$scope.counter = 300;

var mytimeout = null; // the current timeoutID

// actual timer method, counts down every second, stops on zero
$scope.onTimeout = function() {
if ($scope.counter === 0) {
$scope.$broadcast('timer-stopped', 0);
$timeout.cancel(mytimeout);
return;
}
$scope.counter--;
mytimeout = $timeout($scope.onTimeout, 1000);
};

$scope.startTimer = function() {
mytimeout = $timeout($scope.onTimeout, 1000);
};

// stops and resets the current timer
$scope.stopTimer = function() {
$scope.$broadcast('timer-stopped', $scope.counter);
$scope.counter = 30;
$timeout.cancel(mytimeout);
};

// triggered, when the timer stops, you can do something here, maybe show a visual indicator or vibrate the device
$scope.$on('timer-stopped', function(event, remaining) {
if (remaining === 0) {
console.log('your time ran out!');
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>
<div ng-app='TimerApp'>
<div ng-controller="TimerCtrl">
{{counter}}
<button ng-click='startTimer()'>Start</button>
</div>
</div>

上面的 JSFiddle 工作:http://jsfiddle.net/fq4vg/1796/

最佳答案

您可以进行一些小的更改,如下所示。

angular.module('TimerApp', [])
.controller('TimerCtrl', function($scope, $timeout) {
$scope.counter = 300;
$scope.time = Math.floor($scope.counter/60)+':' +$scope.counter % 60;//time representation..
var mytimeout = null; // the current timeoutID

// actual timer method, counts down every second, stops on zero
$scope.onTimeout = function() {
if ($scope.counter === 0) {
$scope.$broadcast('timer-stopped', 0);
$timeout.cancel(mytimeout);
return;
}
// var secs = 300;
$scope.counter--;
//decrement the clock representation...
$scope.time = Math.floor($scope.counter/60)+':' +$scope.counter % 60;
mytimeout = $timeout($scope.onTimeout, 1000);
};

$scope.startTimer = function() {
mytimeout = $timeout($scope.onTimeout, 1000);
};

// stops and resets the current timer
$scope.stopTimer = function() {
$scope.$broadcast('timer-stopped', $scope.counter);
$scope.counter = 30;
$timeout.cancel(mytimeout);
};

// triggered, when the timer stops, you can do something here, maybe show a visual indicator or vibrate the device
$scope.$on('timer-stopped', function(event, remaining) {
if (remaining === 0) {
console.log('your time ran out!');
}
});
});

您的 HTML 将包含以下代码。

<body>
<div ng-app='TimerApp'>
<div ng-controller="TimerCtrl">
{{time}}
<button ng-click='startTimer()'>Start</button>
</div>
</div>

关于javascript - 如何将计时器值格式更改为 MM :Sec from fixed counter in angular js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39455850/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com