简体   繁体   English

切换标签时,角度4设置间隔暂停

[英]angular 4 setinterval pausing when switch tab

My setInterval timer is pausing when leave the tab and resume when switch back to the tab I want solution to make it keep counting when the tab is switched back 我的setInterval计时器在离开标签页时暂停,并在切换回标签页时恢复运行,我希望解决方案使其在切换标签页时继续计数

here is GIF picture shows what happen 这是GIF图片,显示发生了什么

在此处输入图片说明

here is my code: 这是我的代码:

  startCountDown(time) {
    clearInterval(this.countDownInterval);
    this.timeLeft = time * 100;
    if (time === 0) {
      return;
    }

    this.countDownInterval = setInterval(() => {
      this.timeLeft -= 1;
      if (this.timeLeft === 0) {
        clearInterval(this.countDownInterval);
      }
    }, 10);
  }
  updateTimer() {
    if (this.timeLeft > 0) {
      $('.rolling').fadeIn(200);
      $('.rolling-inner').html('<div>' + (this.timeLeft / 100).toFixed(2).replace('.', ':') + '</div>');
    } else {
      $('.rolling').fadeOut(200);
    }
  }
  set timeLeft(x) {
    this._timeLeft = x;
    this.updateTimer();
  }
  get timeLeft() {
    return this._timeLeft;
  }

Setinterval is not misbehaving , This is what it's property . Setinterval不会出现异常,这就是它的属性。 But you can track the tab selection action and add the pending idle duration with existing time. 但是您可以跟踪选项卡选择操作,并将未决的空闲持续时间与现有时间相加。

This is the code to track the tab selection and blur 这是跟踪选项卡选择和模糊的代码

$(window).on("blur focus", function(e) { 
    var prevType = $(this).data("prevType"); 

    if (prevType != e.type) { 
        switch (e.type) { 
            case "blur": break; 
            case "focus":  break; 
        } 
    } 

    $(this).data("prevType", e.type); 
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM