chenjianou / blog

我的个人博客
0 stars 0 forks source link

解决webapp 唤醒后的问题 #30

Open chenjianou opened 3 years ago

chenjianou commented 3 years ago

const ua = window.navigator.userAgent; const system = { IOS: /(iPhone|iPad|iPod|iOS)/gi.test(ua), Android: /android|adr/gi.test(ua) }; if(system.IOS) { let self = this       let time = null; (function fn(ctx) {       const handler =setTimeout(() => {         const now = new Date();         !time && (time = now);         const cost = now - time;         time = now;         if (cost > 3000) {           ctx.$toast('我回来了!'); ctx.socket.init(); ctx.$store.dispatch('setUserInfo')         }; fn(ctx)       }, 1000)     })(this); } else { function getHiddenProp() { var prefixes = ['webkit', 'moz', 'ms', 'o']; // if 'hidden' is natively supported just return it if('hidden' in document) return 'hidden'; // otherwise loop over all the known prefixes until we find one for(var i = 0; i < prefixes.length; i++) { if((prefixes[i] + 'Hidden') in document) return prefixes[i] + 'Hidden'; } // otherwise it's not supported return null; } function isHidden() { var prop = getHiddenProp(); if(!prop) return false;

    return document[prop];
  }
  this.$nextTick(() => {
    document.addEventListener('visibilitychange', (e) => {
      if(isHidden()) {
        this.$toast('我回来了!');
        this.socket.init();
        this.$store.dispatch('setUserInfo')
      }
      // console.log(isHidden() + "-" + new Date().toLocaleTimeString())
    });
  });
}