我有三個功能。在按鈕點擊事件上調用第一個功能,即Start()
。 start()
在counter1
(這個計數器遞減並且我在我的模板上顯示它)指定的特定秒數後調用另一個功能startFilling()
。 startFilling()
應該在另一個counter2
(這個計數器也遞減並且我在我的模板上顯示它)顯示特定秒數後調用另一個函數checkCorrectness()
。Angular2後續計數器
我的HTML:
<!-- Show counter -->
<div align="center" style="margin-bottom:1cm;" *ngIf="counter1">
<h5>Countdown {{counter1}}</h5>
</div>
<div align="center" style="margin-bottom:1cm;" *ngIf="counter2">
<h5>Countdown {{counter2}}</h5>
</div>
我的代碼看起來像以下:
counter1=5;
counter2=null;
myCounter1: any;
myCounter2: any;
start(){
/* Set the counter to fire the startFilling() function after specific seconds */
let timer = Observable.timer(1000,1000);
this.myCounter1 = timer.subscribe(t=> this.startFilling(t));
}
startFilling(counter){
this.counter1=this.counter1-1;
if(this.counter1==0){
this.counter1=null;
alert("Filling finished")
this.myCounter1.unsubscribe();
this.counter2=11;
}
if(this.level===1 || this.level===2 || this.level===3){
let timer = Observable.timer(this.counter2, 1000);
this.myCounter2 = timer.subscribe(t=> this.checkCorrectness(t));
}
if(this.level===4 || this.level===5 || this.level===6 || this.level===7){
let timer = Observable.timer(this.counter2,1000);
this.myCounter2 = timer.subscribe(t=> this.checkCorrectness(t));
}
}
checkCorrectness(counter){
this.counter2=this.counter2-1;
if(this.counter2==0){
this.counter2=null;
alert("Check correctness")
this.myCounter2.unsubscribe();
}
}
這裏的問題是:而此時我只想要一個計數器設置爲在屏幕上可見,在我的情況在某些時候他們都出現了。而且,它們以某種方式重疊並且變爲負面。
我該如何解決這個問題?
「我一次只想在屏幕上顯示一個計數器,在我的情況下,他們都顯示」 - 您能告訴我們HTML代碼嗎? – inspired
我更新了我的問題。現在我試圖移動第二個計時器內部if(this.counter1 == 0){...}'否則當它在外面時,它只是開始執行 – Nitish
你的代碼的目的是什麼?你想在第二個定時器完成後執行計數器2? – inspired