我正在使用angular来实现一个计数器,如下所示:
<button ng-click="order.startOperation(operation)">
<p ng-if="counter.start"></p>
</button>
该按钮调用一个函数来启动计数器。我还有一个ng-if
语句,这样如果计数器启动,用户可以看到分钟和秒运行。计数器启动后,可以使用相同的按钮停止计数器。
function startOperation(operation) {
var operation.lastStart = Date.now();
operation.playing = true;
}
// Pause an operation timer.
function pauseOperation(operation) {
var operation = vm.data.operations[vm.data.operations.indexOf(operation)];
if (!operation.start) {
operation.start = operation.lastStart;
}
operation.end = Date.now();
// Sumarize the transcurred seconds.
operation.partialTime = operation.partialTime || 0;
operation.partialTime += Math.ceil((operation.end - operation.lastStart) / 1000);
dataService.update();
}
答案 0 :(得分:0)
你需要$elapsedTime
变量每隔一秒更新一次:
<button ng-click="order.startOperation(operation)">
<p ng-if="counter.start">{{$elapsedTime}}</p>
</button>
要更新其值,请使用$timeout
服务:
var updateElapsedTime = function () {
$scope.elapsedTime = Date.now() - operation.lastStart;
if (operation.playing) {
// keep updating
$timeout(updateElapsedTime, 1000);
}
}
updateElapsedTime();