Answer the question
In order to leave comments, you need to log in
How to get data periodically?
I'm in an angular6 project trying to fetch data every 5 seconds. Wrote a service, but it does not work.
JSFIDDLE
Please help me fix the code:
getExample(): Observable<any> {
console.log('getExample');
return Observable
.timer(0, 5000)
.flatMap(() => this.httpClient.get('https://jsonplaceholder.typicode.com/posts'))
.map((r) => {
return r;
});
}
Answer the question
In order to leave comments, you need to log in
In RxJS 6 version, the pipe operator was added, in which the stream should be changed
import { Observable, timer } from 'rxjs';
import { flatMap } from 'rxjs/operators';
getExample(): Observable<any> {
console.log('getExample');
return timer(0, 5000).pipe(
flatMap(() => this.httpClient.get('https://jsonplaceholder.typicode.com/posts'))
)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question