Given the following code, how to I alter it to make the get request to "api/foobar" repeat every 500 milliseconds?
import {Observable} from "RxJS/Rx";
import {Injectable} from "@angular/core";
import {Http} from "@angular/http";
@Injectable() export class ExampleService {
constructor(private http: Http) { }
getFooBars(onNext: (fooBars: FooBar[]) => void) {
this.get("api/foobar")
.map(response => <FooBar[]>reponse.json())
.subscribe(onNext,
error =>
console.log("An error occurred when requesting api/foobar.", error));
}
}
Why not try
setInterval(
)?Try this
make sure you have imported import {Observable} from 'rxjs/Rx' if we don't import it we get observable not found error sometimes
working plnkr http://plnkr.co/edit/vMvnQW?p=preview
I recommend the rx-polling library which abstracts a lot of the details provides mechanisms for retries, back-off strategies, and even pause/resume if browser tab becomes inactive.