我需要一個用angular2編寫的秒錶計時器函數,用於我的在線考試應用程序。 它應該顯示在HH:MM:SS格式中。 應該從01:00:00開始,並應在00:00:00在angularJS中倒數計時器功能2
-4
A
回答
1
結束你可以做這樣的:
@Component({
selector: 'my-app',
template: `
<div>
<h2>Hello {{name}}</h2>
<button (click)="buttonClicked()">{{ started ? 'reset' : 'start' }}</button>
<br />
<span>{{ time.getHours() }}</span>:
<span>{{ time.getMinutes() }}</span>:
<span>{{ time.getSeconds() }}</span>
</div>
`,
})
export class App {
name:string;
started = false;
time = new Date(2000, 1, 1, 1, 0, 0);
constructor() {
this.name = 'Angular2'
this._timerTick();
}
private _timerTick() {
if (this.started) {
this.time.setSeconds(this.time.getSeconds(), -1);
}
setTimeout(() => this._timerTick(), 1000);
}
buttonClicked() {
if (this.started) this.reset();
else this.start();
}
start() {
this.started = true;
}
reset() {
this.started = false;
this.time = new Date(2000, 1, 1, 1, 0, 0);
}
}
現場演示:https://plnkr.co/edit/BkMkAuSoqVgQMhqEKAAg?p=preview
但也有像總是以多種方式實現目標! :)
+0
非常感謝你.... –
+0
接受爲答案請幫助如果它:) – mxii
0
的Html上倒數計時功能被觸發 元素按鈕...
<div class="form-group col-sm-3" [ngClass]="{'red':activate, 'white':!activate, 'blink_me':blink}"><i class="fa fa-clock-o" aria-hidden="true"></i> Time Left: <span>{{currentHour}}:{{currentMinute}}:{{currentSeconds}}</span></div>
<button class="btn btn-primary btn-sm" (click)="_timerTick()">Start Timer</button>
完成倒數計時功能放在這裏在下面的代碼(請 包括這一段代碼在您的計時器組件類中)
constructor(
private examService: ExamService,
private resultService: ResultService,
private activatedRoute: ActivatedRoute,
private route: Router) {
this.start(); //here our countdown timer function gets called by setting the boolean variable **this.started** as true.
}
start() {
this.started = true;
}
private _timerTick() {
if (this.started) {
if (localStorage.getItem('sec')) { // here it checks, if browser local-storage has key **sec** recorded. Basically, when browser gets refreshed by user on that case it used to store key **sec** in local-storage and continue countdown ticking without starting from the beginning.
this.time = new Date(localStorage.getItem('sec'));
this.time.setSeconds(this.time.getSeconds(), -1);
}
else {
this.time = new Date(2017, 2, 24, 0, 0, this.timeInSeconds);
this.time.setSeconds(this.time.getSeconds(), -1);
}
if (this.time.getHours() === 0 && this.time.getMinutes() === 0 && this.time.getSeconds() === 0) {
localStorage.removeItem('sec');
this.started = false;
this.saveData('Time Over!');
}
this.currentHour = this.time.getHours();
this.currentHour = ("0" + this.currentHour).slice(-2); //Here it check for two digits. If it is single then it adds 0 as prefix to it.
this.currentMinute = this.time.getMinutes();
this.currentMinute = ("0" + this.currentMinute).slice(-2);//Here it check for two digits. If it is single then it adds 0 as prefix to it.
this.currentSeconds = this.time.getSeconds();
this.currentSeconds = ("0" + this.currentSeconds).slice(-2);//Here it check for two digits. If it is single then it adds 0 as prefix to it.
if (this.currentHour == 0 && this.currentMinute < 5) {// This line states, if only 5minutes left out then timer get started blinking. Blink code given in below **css** code section
this.activate = true;
this.blink = true;
}
if (this.currentHour == 0 && this.currentMinute == 0 && this.currentSeconds == 0) {// If time is up then we would remove complete browser cache using below lines of code and also deactivate the timer blinking.
localStorage.removeItem('sec');
localStorage.setItem('examTaken', this.name);
this.activate = false;
this.blink = false;
}
else {
localStorage.setItem('sec', this.time);// If still some time's left out then it'll continuously keep updated with browser localStorage
}
}
setTimeout(() => this._timerTick(), 1000);// For every 1sec timer gets updated here
}
下面給出的Timer Blink的CSS代碼。
.blink_me {
animation: blinker 1s linear infinite;
}
@keyframes blinker {
50% {
opacity: 0;
}
}
.white {
color: white;
}
.red {
color: #ff0000;
}
相關問題
- 1. JS倒數計時器 - 暫停功能
- 2. Jquery倒計時功能
- 3. 爲什麼我的計時器功能不能倒計時?
- 4. 倒計時 - iPhone倒數計時器
- 5. 修復倒計時倒數計時器
- 6. AngularJS多倒計時
- 7. 在Sencha Touch 2中顯示倒數計時器2
- 8. 當功能調用時,jquery倒計時重置計時器
- 9. IE中的jQuery倒計時功能
- 10. C#timer_Tick()倒計時2步倒計時
- 11. 倒計時功能實際上並不倒計時
- 12. 倒數計時器
- 13. 倒數計時器?
- 14. 倒數計時器
- 15. 倒數計時器
- 16. 倒數計時器在J2EE
- 17. 倒數計時器在PHP
- 18. 在php倒數計時器?
- 19. 計時器不能正確倒計時
- 20. AngularJS在ng-repeat中的倒計時
- 21. 在Android應用程序中繪製「倒數計時器」倒數計時器
- 22. 如何用新方法擴展JavaScript倒數計時器功能?
- 23. 爲Excel功能區創建倒數計時器
- 24. 定時器倒計時角2
- 25. 調用C#功能時,AJAX定時器(倒計時)顯示0
- 26. angularjs多個倒計時
- 27. 倒計時按鈕angularjs
- 28. 連接到ajax的倒計時功能
- 29. 倒計時功能的恢復
- 30. 如何功能化倒計時?
與JS創建,並將努力在一定angular2 – anshuVersatile
你在哪裏卡住了? – 2017-02-23 07:27:35
幫助我找到在angular2中停止計時的方式。 –