gpt4 book ai didi

javascript - typescript rxjs : is there any clean alternative to setimeout 0

转载 作者:行者123 更新时间:2023-12-02 21:34:56 25 4
gpt4 key购买 nike

在我的 Angular 应用程序中,我正在使用这种处理方法

myMethod(){
...
setTimeout(() => {
this.router.navigate(['mucomponent']);
});
}

正如我被告知的:setTimeout 没有延迟 (0) 似乎等待下一个刻度,所有治疗完成后开始我的治疗。这是近期的计划。

因为我需要这种行为

是否有任何干净的替代方法可以与 typescriptrxjs 做同样的事情,以做得更好?

建议?

最佳答案

尽管接受的答案在技术上可能可行,但它远非理想。 RxJS 团队正是为此用例创建了 asapScheduler

asapScheduler.schedule(() => this.router.navigate(['mucomponent']));

来自RxJS docs :

asap scheduler will do its best to minimize time between end of currently executing code and start of scheduled task. This makes it best candidate for performing so called "deferring". Traditionally this was achieved by calling setTimeout(deferredTask, 0), but that technique involves some (although minimal) unwanted delay.

关于javascript - typescript rxjs : is there any clean alternative to setimeout 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60524851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com