私には3つの機能があります。最初の機能、すなわちStart()
がボタンクリックイベントで呼び出されます。 start()
は、counter1
()によって設定された特定の秒後に別の関数startFilling()
を呼び出します。このカウンタが減少し、テンプレートに表示されます。 startFilling()
は別の()によって設定された特定の秒後に別の関数checkCorrectness()
を呼び出す必要があります。このカウンタも減少し、テンプレートに表示されます。角2の後続カウンタ
マイ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 tempateコードを表示することができますか? – inspired
私の質問が更新されました。今、私は 'if(this.counter1 == 0){...}'の中に2番目のタイマーを移動しようとしています。そうでなければ、それは単に実行を開始します – Nitish
あなたのコードの目的は何ですか? 2番目のタイマーが終了したらカウンタ2を実行しますか? – inspired