Angular rxjs Observable.interval()在Chrome的背景标签中无法正确触发 [英] Angular rxjs Observable.interval() not triggering properly on background tabs in Chrome

查看:179
本文介绍了Angular rxjs Observable.interval()在Chrome的背景标签中无法正确触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写带有通过RxJs observables实现的间隔计时器的angular2应用程序,并且刚刚注意到当Tab处于后台时,Chrome浏览器中Observable.interval()和Observable.timer()的奇怪行为. 角组件应在控制台中每秒打印一次秒数,但在后台选项卡上却无法按预期工作-函数每x + 1秒触发一次,其中x是在间隔函数中明确指定的间隔

I am writing angular2 application with interval timers implemented via RxJs observables, and just noticed the strange behaviour of Observable.interval() and Observable.timer() in Chrome browser when tab is in background. Angular component should once every second print number of seconds in console, but on background tabs, this is not working as intended - function triggers every x+1 seconds, where x is interval specified explicitely in the interval funciton

角度组件代码:

ngOnInit() {
  let a = Observable.interval(1000).subscribe(() => {
    let date = new Date();
    console.log(date.getSeconds());
  });
}

示例:选项卡1(具有上面定义的计时器的选项卡)上的控制台输出:

37 <- tab1 (with timer)
38
39
40
41 <- changed tab to tab2
43
45
47
49
51
53
55
57
59 <- changed tab to tab1
0
1
2
3

Mozzila FF没问题.

There is no problem with Mozzila FF.

我认为此行为是浏览器中背景选项卡优先级较低的结果,但是为什么间隔总是总是推迟一秒钟?

I presume that this behaviour is consequence of lower priority of background tabs in browser, but why is the interval always postponed by just one second?

推荐答案

但是,如果使用setInterval(),则在任何浏览器中都不会出现问题.

However, if setInterval() is used, there is no problem in any browser.

ngOnInit() {

  setInterval(()=>{

    console.log(new Date())
  },1000)  
}

这篇关于Angular rxjs Observable.interval()在Chrome的背景标签中无法正确触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆