Hello every one, here is my problem I set an interval but it never stops even if I call clearInterval(x) :
:code: getMatches() { let dataToBeSentBack; let matchesPromise; return new Observable(observer => { let interval = setInterval(() => { matchesPromise = this.http.get(this.matchesURL) .toPromise() .then(response => {observer.next(response.json());}) .catch(this.handleError); }, 3000); return (interval) => { console.log("inside the return of the observable"); clearInterval(interval); } }); } ngOnInit(){ let matchesServiceDisposable = this.matchesService.getMatches().subscribe( data => {this.matches = data;}, err => this.showError(err) ); setTimeout(() => { console.log("called the unsubscribe"); matchesServiceDisposable.unsubscribe(); }, 10000); }
console output :
matches.js:29 called the unsubscribe
matches.service.js:31 inside the return of the observable
the onNgInit() function is called by the system and this last one subscribe to the observable returned by the service on other terms onNgInit call getMatches() automatically just 1 time.
thanks for your help