Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

timer.unsubscribe is not a function Angular2

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.

What am I doing wrong?

    let timer:any = Observable.timer(0,1000);
    timer.subscribe(data => {
            console.log(this.itemsRatedList);
            if(data == 5) timer.unsubscribe();
        });
like image 557
s.spirit Avatar asked Dec 03 '22 23:12

s.spirit


1 Answers

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

like image 167
Günter Zöchbauer Avatar answered Jan 22 '23 14:01

Günter Zöchbauer