【问题标题】:Angular2: create and destroy timerAngular2:创建和销毁计时器
【发布时间】:2016-09-18 12:54:50
【问题描述】:

我正在尝试创建计时器,它将每 5 秒发送一次 GET 请求,我设法做到了,但我注意到如果我移动到不同的页面(路由),计时器仍在运行,所以我尝试添加 ngOnDestroy,但我没有“取消订阅”方法

import {Component, OnInit, OnDestroy} from '@angular/core';
import {Observable} from 'rxjs/Rx';

@Component({
  templateUrl: 'app/pages/CurrentRuns/currentruns.component.html'
})




export class CurrentRunsComponent implements OnInit, OnDestroy {
  private timer;
   ticks=0;
    

  ngOnInit() {
    this.timer = Observable.timer(2000,5000);
    this.timer.subscribe(t => this.tickerFunc(t));
  }
    tickerFunc(tick){
    console.log(this);
    this.ticks = tick
  }

   ngOnDestroy(){
    console.log("Destroy timer");

  }
}

  

我正在使用 angular2 RC7,“rxjs”:“5.0.0-beta.12”

【问题讨论】:

    标签: angular observable


    【解决方案1】:

    订阅一个可观察对象返回一个订阅对象

    import {Component, OnInit, OnDestroy} from '@angular/core';
    import { Observable, Subscription } from 'rxjs/Rx';
    
    @Component({
        templateUrl: 'app/pages/CurrentRuns/currentruns.component.html'
    })
    export class CurrentRunsComponent implements OnInit, OnDestroy {
        ticks = 0;
        private timer;
        // Subscription object
        private sub: Subscription;
    
        ngOnInit() {
            this.timer = Observable.timer(2000,5000);
            // subscribing to a observable returns a subscription object
            this.sub = this.timer.subscribe(t => this.tickerFunc(t));
        }
        tickerFunc(tick){
            console.log(this);
            this.ticks = tick
        }
    
        ngOnDestroy(){
            console.log("Destroy timer");
            // unsubscribe here
            this.sub.unsubscribe();
    
        }
    }

    【讨论】:

    • 你好我有一个问题,当我访问另一个页面时它会停止但是当我回到这个页面时会启动 2 个计时器。
    【解决方案2】:

    在我的情况下,当我访问另一个页面时它会停止,但是当我回到这个页面时,会启动 2 个计时器,就像上面评论中的 @user3145373 所说的那样。

    所以解决方案是在 ngOnDestroy 中将 timer 的值设为 null,如下所示:

    export class CurrentRunsComponent implements OnInit, OnDestroy {
        ticks = 0;
        private timer;
        // Subscription object
        private sub: Subscription;
    
        ngOnInit() {
            this.timer = Observable.timer(2000,5000);
            // subscribing to a observable returns a subscription object
            this.sub = this.timer.subscribe(t => this.tickerFunc(t));
        }
    
        tickerFunc(tick){
            console.log(this);
            this.ticks = tick
        }
    
        ngOnDestroy(){
            console.log("Destroy timer");
            // unsubscribe here
            this.sub.unsubscribe();
            this.timer = null
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      只需退订

      ngOnDestroy() {
          this.myObservable.unsubscribe();
      }
      

      其中myObservablesubscribe() 返回的对象。

      参考:angular2 - Best way to terminate a observable interval when path changes

      【讨论】:

      • 你的意思是:this.timer.unsubscribe(); - 因为我没有收到 unsubscribe();在输入 this.timer 时。
      • 创建一个变量“私人订阅者对象:订阅;”然后分配它“this.subscriberObj = this.timer.subscribe(t => this.tickerFunc(t));”最后分配给“ngOnDestroy(){this.subscriberObj.unsubscribe();}”
      猜你喜欢
      • 2015-07-15
      • 2016-12-06
      • 2016-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      相关资源
      最近更新 更多