我有一個很長的后端行程(30 分鐘到 1 小時),它在 Angular 中被呼叫。
只有在此程序運行時,我想以 10 分鐘的時間間隔向后端發送不同的請求。
這是長 API 呼叫的示例
this.someService
.apiCall(passedData)
.subscribe((response:any) => {
//Other code here
},
(error: HttpErrorResponse) => {
alert(error.message);
}
);
這是我想在上述情況仍在進行時每 10 分鐘打一次的電話
this.otherService.otherApiCall().subscribe((result)=> {}, () => {});
我不確定我是否應該有一些特殊的 RxJS 代碼來編輯訂閱,或者有不同的解決方案。
我已經有一個 HttpInterceptorService 在行程運行時用作加載微調器,但我認為我不會為特定的 API 呼叫修改此代碼來實作我正在尋找的內容。
uj5u.com熱心網友回復:
我想出了解決這個問題的方法。我在我的組件中添加了以下內容。
private alive: boolean = false;
private count = 0;
stayAlive(){
this.alive = true;
timer(0, 900000) //runs initially and every 15 minutes
.pipe(
takeWhile(() => this.alive)
)
.subscribe(() => {
this.count = this.count 1;
console.log('RAN API : ' this.count);
this.otherService.otherApiCall().subscribe((result)=> {}, () => {});
});
}
然后是我的長 API 呼叫看起來像這樣。
methodToCall(){
this.stayAlive();
this.someService
.apiCall(passedData)
.subscribe((response:any) => {
//Other code here
},
(error: HttpErrorResponse) => {
alert(error.message);
},() =>{
this.alive = false;//will stop running API call every 15 minutes
}
);
}
我也添加了這個
ngOnDestroy(){
this.alive = false;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/524265.html
上一篇:React組件在請求查詢中發送[objectObject]
下一篇:Python請求文字與計算
