[英]How to delay a method call after 1 min in angular or rxjs using Delay operator
嗨,我的情況是我只需要在 1 分鍾后調用一個方法,我也可以設置超時,但是否可以使用如下所示的延遲方法
getMethod(){
}
delay(1000,this.getMethod())
或任何其他更好的方法
如果您使用 rxjs 和 Angular,那么您可能想使用 RxJs timer
主題
import { timer } from 'rxjs';
const minute = 1000 * 60;
function helloWorld() {
console.log('hello');
}
timer(minute).subscribe(helloWorld);
如果您正在尋找想要的 RxJs 主題或運算符,請嘗試查看RxJs 運算符決策樹以獲取幫助。
您可以使用 rxjs 延遲管道:
getMethod(): Observable {
return of(2)
}
const delayedMethod = getMethod.pipe(delay(1000))
delayedClicks.subscribe(x => console.log(x));
您可以使用 promise 和 async-await 來實現這一點。 檢查下面的代碼
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}
await sleep(2000) // This will give 2 seconds delay
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.