我正在嘗試進行 5 分鐘倒計時以觸發服務方法并重新加載 5 分鐘倒計時(基本上我試圖每 5 分鐘重新加載一些資料)
constructor(
private reloadDataService: ReloadDataService,
private userService: UserService
) {
this.timer$ = timer(0, 1000).pipe(
scan(acc => --acc, 300),
takeWhile(x => x >= 0),
);
}
realod 方法由按鈕觸發或每 5 分鐘觸發一次
reload() {
this.lastRefresh = new Date();
this.reloadDataService.reload()
}
ngOnInit() {
this.getUserDetails();
}
我試過
this.timer$ = timer(0, 1000).pipe(
switchMap(() => this.reload()),
scan(acc => --acc, 300),
takeWhile(x => x >= 0),
);
但沒有用 - 如何每 5 分鐘觸發一次重新加載方法?
uj5u.com熱心網友回復:
使用定時器的訂閱屬性,
const source = timer(0, 300000);
source.subscribe((_) => this.reload());
uj5u.com熱心網友回復:
試試下面的代碼。它將設定間隔方法,當訂閱refreshInterval$ 時,您可以呼叫 reload 方法或任何您想要重復的操作。
const refreshInterval$: Observable<any> = timer(0, 300000)
.pipe(
// Boolean to kill the request if the user closes the component
takeWhile(() => this.killTrigger));
refreshInterval$.subscribe(() => {
// Your code here
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/334771.html
標籤:javascript 有角的 计时器 rxjs
