Angular2 可观察定时器条件

Angular2 Observable Timer Condition

我有一个计时器:

initiateTimer() {
    if (this.timerSub)
        this.destroyTimer();

    let timer = TimerObservable.create(0, 1000);
    this.timerSub = timer.subscribe(t => {
        this.secondTicks = t
    });
}

如何将条件添加到 60 分钟后向用户显示弹出窗口?我已经尝试查看几个问题 (this and ),但它不适合我。 RxJS 模式仍然是新手...

你不需要 RxJS。您可以使用旧 setTimeout:

initiateTimer() {
    if (this.timer) {
        clearTimeout(this.timer);
    }

    this.timer = setTimeout(this.showPopup.bind(this), 60 * 60 * 1000);
}

如果你真的必须使用 RxJS,你可以:

initiateTimer() {
    if (this.timerSub) {
        this.timerSub.unsubscribe();
    }

    this.timerSub = Rx.Observable.timer(60 * 60 * 1000)
        .take(1)
        .subscribe(this.showPopup.bind(this));
}

只需使用 observable.timer 并订阅即可。

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");
    });
  }
}

Please see more details

我最终从我最初拥有的东西开始做这件事,这给了我我需要的东西:

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;
        }
    });
}

我在尝试我标记为答案的内容之前实现了这个,所以就把它留在这里。