解决切后台后定时器继续问题

// 解决定时器切后台时间停止问题(that.orderData.valid_order_period 此为页面显示定时器)

    addVisibilitychange() {
      let that = this;
      let start, end, s;
      document.addEventListener('visibilitychange', function() {
        if(document.visibilityState == 'hidden') {
          start = new Date().getTime();  //开始切后台时间戳
        }else if(document.visibilityState == 'visible') {
          end = new Date().getTime();   //切后台之后返回显示的时间戳
          s = Math.floor((end - start)/1000);
          that.orderData.valid_order_period = that.orderData.valid_order_period - s;
          if(that.orderData.valid_order_period <= 0) {
            that.orderData.valid_order_period = 0;
            window.clearInterval(that.timer);
          }
        }else { }
      })

你可能感兴趣的:(解决切后台后定时器继续问题)