首页 > 解决方案 > 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.

标签: timerrxjsintervalspolling

解决方案


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.


推荐阅读