ngAfterViewInit(){
Observable.interval(3000).timeInterval().subscribe()=>{};
}
Trying to invoke the Observable.interval() method it is
this is correct for angular 6.1.+ and rxjs 6.2.+
import { Observable } from 'rxjs';
import { interval } from 'rxjs';
interval(1000).subscribe(
(value: number) => {
this.secondes = value;
},
(error: any) => {
console.log('error');
},
() => {
console.log('observable completed !');
}
);
for rxjs 5.5.2+ it is:
import { interval } from 'rxjs/observable/interval';
usage:
interval(3000).subscribe(x => // do something)
import { Observable } from 'rxjs';
import { interval } from 'rxjs';
import { takeWhile } from 'rxjs/operators';
// here 20 (secs) is a reference till when it will get completed
interval(1000).pipe(takeWhile(value => value < 20)).subscribe(
(value: number) => {
this.secondes = value;
},
(error: any) => {
console.log('error');
},
() => {
console.log('observable completed !');
}
);
This is a answer to how to unsubscribe.
For RxJS 6+ the answer given by Tomasz Kula only applies when using the rxjs-compat
package, which should only be used when in the process of converting an application from RxJS 5 to RxJS 6.
Within RxJS 6+, use:
import { interval } from 'rxjs';
interval(3000).subscribe(x => /* do something */)
Note that any Observable
creation function that previously existed on the Observable
type, should now be imported from 'rxjs'
.