timer - RxJS interval() varies wildly in MS Edge
问题描述
I have an Angular 6 application that performs an API call every 10 seconds to update price quotes. The timing of the API calls is manages using RxJS interval().
For some reason, on MS Edge, the timings vary wildly, from a couple of seconds, to minutes. Any ideas what might be the cause?
Here is the code:
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);
})
)
Here is the console output:
0.001
18.143
4.111
11.057
13.633
12.895
3.003
12.394
7.336
31.616
20.221
10.461
Is there a way to increase the accuracy?
EDIT:
Performance degrades over time.
Reducing the code in the interval() to only a console.log does not perform any better.
Might be an Angular issue.
解决方案
It is up to the browser to decide how many CPU cycles are allocated per browser tab. Depending on resources (for instance; battery) or activity (background tab vs foreground tab) your browser page will receive more or less cpu slices.
some background: https://github.com/WICG/interventions/issues/5
This is shipped in Edge as well now (as of EdgeHTML14). The clamping to 1Hz in background tabs, not anything more intensive.
Apart from this fact; you are measuring the latency of your call as well:
timeseriesService.getQuote()
so it might also be that this call just takes some time.
推荐阅读
- javascript - 使用 javascript 编辑内联样式
- javascript - 节点:在构造后添加 catch-function 到 promise
- java - 尝试在模拟器上运行我的应用程序时出错:>任务执行失败:app:transformClassesWithJarMergingForDebug
- python - QtOpenGL - 共享 OpenGL 上下文
- python - Pandas DataFrame中“Value”一词的作用
- android - 创建片段后预期的元素
- unit-testing - 如何使用 REQUIRES_NEW 和 DB 中的约束为服务方法编写 UnitTest
- docusignapi - 我正在尝试进行 Docusign API 调用
- java - 更改 ListView 中单个项目的 TextView
- mysql - mysql 或 mariadb 是带有分隔符的单行语句吗?