【发布时间】:2020-11-26 17:17:23
【问题描述】:
我需要一个秒表,以 hh:mm:ss 格式显示时间
【问题讨论】:
标签: angular typescript timer
我需要一个秒表,以 hh:mm:ss 格式显示时间
【问题讨论】:
标签: angular typescript timer
这是我研究了一段时间但没有找到我想要的确切类型后制作的秒表。希望这可以帮助有同样问题的人。时间以 hh:mm:ss 格式显示在屏幕上,如果需要更改在 getDisplayTimer() 方法中
import { timer } from 'rxjs';
//timer
public displayTimer;
public isRunning: boolean = false;
public startText = 'Start';
public time;
ngOnInit(): void {
this.time = 0;
}
toggleTimer() {
this.isRunning = !this.isRunning;
this.stopwatch();
}
stopwatch() {
timer(0, 1000).subscribe(ellapsedCycles => {
if (this.isRunning) {
this.time++;
this.getDisplayTimer(this.time);
this.startText = 'Pause';
} else {
this.startText = 'Start';
}
});
}
getDisplayTimer(time: number) {
var hours = '' + Math.floor(time / 3600);
var minutes = '' + Math.floor(time % 3600 / 60);
var seconds = '' + Math.floor(time % 3600 % 60);
if (Number(hours) < 10) {
hours = '0' + hours;
} else {
hours = '' + hours;
}
if (Number(minutes) < 10) {
minutes = '0' + minutes;
} else {
minutes = '' + minutes;
}
if (Number(seconds) < 10) {
seconds = '0' + seconds;
} else {
seconds = '' + seconds;
}
this.displayTimer = hours + ':' + minutes + ':' + seconds;
}
<div class="row">
<div class="col-lg-4 m-b-30">
<section class="timer-counter-label">
<div *ngIf="displayTimer; else elseBlock"> {{displayTimer}} </div>
<ng-template #elseBlock> 00:00:00 </ng-template>
</section>
<section class="timer-button-container">
<button class="btn btn-outline-light" (click)="toggleTimer()">
{{ startText }}
</button>
</section>
</div>
</div>
【讨论】: