timer.unsubscribe is not a function Angular2

后端 未结 2 1521
无人及你
无人及你 2021-01-13 14:42

Tried to make a simple timer and need to break it on some if condition. But always got an error EXCEPTION: timer.unsubscribe is not a function.

2条回答
  •  醉梦人生
    2021-01-13 15:14

    It should be:

    let timer:any = Observable.timer(0,1000);
    let subscription = timer.subscribe(data => {
      console.log(this.itemsRatedList);
      if(data == 5) 
        subscription.unsubscribe();
    });
    

    You can't unsubscribe an Observable, only a Subscription.

    Plunker example

提交回复
热议问题