tengo un temporizador:
initiateTimer() { if (this.timerSub) this.destroyTimer(); let timer = TimerObservable.create(0, 1000); this.timerSub = timer.subscribe(t => { this.secondTicks = t }); }¿Cómo agregaría la condición después de 60 minutos para presentar una ventana emergente al usuario? Intenté ver un par de preguntas ( this y this ) pero no me funciona. Todavía nuevo en los patrones RxJS...
No necesitas RxJS para eso. Puedes usar el viejo setTimeout :
initiateTimer() { if (this.timer) { clearTimeout(this.timer); } this.timer = setTimeout(this.showPopup.bind(this), 60 * 60 * 1000); }Si realmente debe usar RxJS, podría:
initiateTimer() { if (this.timerSub) { this.timerSub.unsubscribe(); } this.timerSub = Rx.Observable.timer(60 * 60 * 1000) .take(1) .subscribe(this.showPopup.bind(this)); }Simplemente use observable.timer y suscríbase.
import { Component } from '@angular/core'; import { Observable } from 'rxjs/Rx'; @Component({ selector: 'app-root', templateUrl: './app.component.html', }) export class AppComponent { title = 'app works!'; constructor(){ var numbers = Observable.timer(10000); // Call after 10 second.. Please set your time numbers.subscribe(x =>{ alert("10 second"); }); } }Terminé haciendo esto a partir de lo que tenía inicialmente, lo que me da lo que necesito:
initiateTimer() { if (this.timerSub) this.destroyTimer(); let timer = TimerObservable.create(0, 1000); let hour = 3600; this.timerSub = timer.subscribe(t => { this.secondTicks = t; if (this.secondTicks > hour) { alert("Save your work!"); hour = hour * 2; } }); }Implementé esto antes de probar lo que marqué como respuesta, así que lo dejaré aquí.