RxJS interval() 在 MS Edge 中变化很大



我有一个 Angular 6 应用程序,它每 10 秒执行一次 API 调用来更新报价。 API 调用的计时使用 RxJS interval(( 进行管理。

出于某种原因,在MS Edge上,时间变化很大,从几秒钟到几分钟不等。 任何想法可能是什么原因?

这是代码:

const refreshPriceInterval = interval(10000);
let startTime = new Date(), endTime: Date;
refreshPriceInterval.pipe(
startWith(0),
flatMap(() => {
endTime = new Date();
let timeDiff = endTime.getTime() - startTime.getTime(); //in ms
// strip the ms
timeDiff /= 1000;
console.log(timeDiff);
startTime = new Date();
return this.timeseriesService.getQuote(symbol, this.userInfo.apiLanguage);
})
)

以下是控制台输出:

0.001
18.143
4.111
11.057
13.633
12.895
3.003
12.394
7.336
31.616
20.221
10.461

有没有办法提高准确性?

编辑:

性能会随着时间的推移而降低。

将 interval(( 中的代码减少到仅控制台.log不会执行任何更好的性能。

可能是角度问题。

由浏览器决定为每个浏览器选项卡分配多少个 CPU 周期。 根据资源(例如电池(或活动(背景选项卡与前台选项卡(,浏览器页面将收到或多或少的 CPU 切片。

一些背景:https://github.com/WICG/interventions/issues/5

这现在也包含在Edge中(从EdgeHTML14开始(。在背景标签中钳位到 1Hz,没有更密集的。

除了这个事实;你也在测量你的呼叫延迟:timeseriesService.getQuote(),所以这个调用也可能只需要一些时间。

这确实是一个 Angular 问题。 定时进程会导致 Angular 不断重新渲染应用程序,这可能会占用大量资源。

我使用 runOutsideAngular(( 来规避这个问题。 事实证明,我只需要在 Angular 之外运行一个函数:

this.ngZone.runOutsideAngular(() => {
this.handleMouseEvents();
});

感谢Mark van Straten的投入!

最新更新