【发布时间】:2017-05-30 05:10:34
【问题描述】:
给定以下代码,如何更改它以使“api/foobar”的获取请求每 500 毫秒重复一次?
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));
}
}
【问题讨论】:
-
我在stackoverflow.com/a/42659054/2398593 回答了一个类似的问题,问题是我没有使用
interval,因为如果您的通话时间超过 500 毫秒,另一个将被解雇。我的回答解决了这个问题:)
标签: angular typescript rxjs observable polling