有沒有什麼辦法可以每隔1分鐘回覆一次承諾? 我試圖這樣的事情,但它一開始只有一次返回承諾:每1分鐘回覆一次?
startWork() {
this.dataService.startPing(details).then((result) => {
this.timeSlotsRefresh();
}, (err) => {
console.log(err);
});
}
然後:
startPing() {
let startingTime = new Date();
return new Promise((resolve, reject) => {
let source = Rx.Observable.timer(startingTime, 60000).timeInterval().pluck('interval');
this.Subscription = source
.subscribe(data => {
this.http.post('http://localhost:63203/api/Ping', JSON.stringify(this.offlinePings[i]))
.map(res => res.json())
.subscribe(data => {
resolve(data);
}, (err) => {
reject(err);
});
});
});
}
它基本上告知功能,每1分鐘請致電this.timeSlotsRefresh();
刷新數據,我該如何實現?
爲什麼不使用'setInterval'呢? –
你能告訴我一個例子嗎? – ChristoK
https://stackoverflow.com/questions/35592716/making-polling-request-in-angular-2-using-observable –