2017-08-31 7 views
1

私は試験アプリを作成しているので、時間がたつと、時間を超過した結果をユーザーに伝えて結果ページに表示するために、 !ANGULAR:Timerから退会した後に別の機能を注入するObservable

しかし、タイマーから退会した後、私の機能はループに入ります!

私はこれで助けが必要です。

マイコード!

minutesDisplay: number = 0; 
hoursDisplay: number = 0; 
secondsDisplay: number = 0; 

sub: Subscription; 

showAlert() { 
    let alert = this.alertCtrl.create({ 

     subTitle: 'Ooops, Time Up! ', 
    }); 
    alert.present(); 
    this.activity.openModal(); 
    } 

ngOnInit() { 
    this.startTimer(); 
} 

public ngOnDestroy() { 
    if (this.minutesDisplay == 1){ 
     this.sub.unsubscribe(); 
    } 

    return true; 

} 

private startTimer() { 
    let timer = Observable.timer(1, 1000); 
    this.sub = timer.subscribe(
     t => { 
      this.ticks = t; 
      this.secondsDisplay = this.getSeconds(this.ticks); 
      this.minutesDisplay = this.getMinutes(this.ticks); 
      this.hoursDisplay = this.getHours(this.ticks); 
      this.ngOnDestroy(); 
     } 
    ); 
    this.showAlert(); 
} 
+0

あなたがplunkerを設定することができ、どのようにループを参照してください?アラートは常に表示されますか? – Vega

+0

はい、ここではプランナーが非常に役立ちます。それ以外の場合は、構文を推測しています。 – DeborahK

答えて

1

私はこれを試していない...しかし、あなたは次のことを試みることができる:?

public ngOnDestroy() { 
    this.sub.unsubscribe(); // Stop the timer if the user navigates elsewhere 
} 

private startTimer() { 
    let timer = Observable.timer(1, 1000); 
    this.sub = timer.subscribe(
     t => { 
      this.ticks = t; 
      this.secondsDisplay = this.getSeconds(this.ticks); 
      this.minutesDisplay = this.getMinutes(this.ticks); 
      this.hoursDisplay = this.getHours(this.ticks); 
      if (this.minutesDisplay == 1){ 
       this.sub.unsubscribe(); // Stop the timer when time is up. 
       this.showAlert();  // Show the alert now 
      } 
     } 
    ); 
} 
+0

私は同じことを考えていました:) – Vega

+0

@DeborahKは同じループをまだ返しています –

+0

これがどのように機能しているかをよく調べるには、本当にプランナーを用意する必要があります。このサイトに行く:https://plnkr.co/edit/?p=catalogue大きな緑色の新しいボタンからリストをドロップダウンし、「角度」を選択します。次に、生成されたファイルにコードを追加します。問題をそこに示すことができれば、コードを使って問題を解決することができます。 – DeborahK

0
Observable.timer(1, 1000) 
    .map(t=>this.getMinutes(t) === 1) // Observable<tick> -> Observable<boolean> 
    .first(istimeUp => istimeUp) // take first Observable<true> 
    .subscribe(()=>this.showAlert()) 
関連する問題