I am making a simple angular2 toy project that hits a URL every 5 seconds. Right now I have it so that it polls the URL, and when the document loads, it does not wait. But that seems really awkward. Is there a more elegant solution?
Observable.interval(1000 * 5) .flatMap(() => this.http.get(url)) .merge(this.http.get(url)) // Merges a stream that starts right away!!! .map((res:Response) => res.json());
Instead of timer you can use
timer
Observable.timer(0, 1000 * 5) .flatMap(() => this.http.get(url), (_, res) => res.json);
timer takes an initial delay before releasing its first event, and then emits just like interval every 5 seconds.
interval
I think you could try something like this:
Observable.interval(1000 * 5) .startWith(0) .flatMap(() => this.http.get(url)) .map((res:Response) => res.json());
This will instantly produce a value, and then once every 5 seconds.
Check out the documentation here.
Source: https://habr.com/ru/post/1247191/More articles:Error: TABLE_QUERY expressions cannot query BigQuery tables - google-bigqueryConverting json array to json object in android using gson? - javaWhat is the difference between `pushManager.subscribe` and` pushManager.getSubscription` Service Worker's - google-chromeStarting a new line after connecting to the command: is there any standard? - unixHow to remove the last CR char with `cut` - linuxTypeScript: is there something like a __FILE__ compiler macro? - typescriptHow to add list names in df as a column in a data frame - reclipse with moonrise color theme: change button color - eclipseSASS equivalent to LESS Space syntax ("+ _") - sassHow to display information inside a module from url? - javascriptAll Articles