js监听用户思否在当前页面

    (function () {
      var t
      var hiddenProperty = 'hidden' in document ? 'hidden' :
        'webkitHidden' in document ? 'webkitHidden' :
        'mozHidden' in document ? 'mozHidden' :
        null;
      var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
      var onVisibilityChange = function () {
        document.title = ''

        if (!document[hiddenProperty]) {
          var arr = '喜欢这个效果就收藏吧'
          console.log(arr.length)
          var index = 0
          t = setInterval(() => {
            if (index > arr.length) {
              index = 0;
              document.title = ' '
            }
            var oldTitle = index === 0 ? '' : document.title.slice(0, document.title.length - 1)
            document.title = oldTitle + arr.slice(index, index + 1) + '_'
            index++
          }, 300);
        } else {
          console.log(t)
          if (t) {
            console.log('clear')
            clearInterval(t)
          }
          document.title = '快看,又出bug了'
        }
      }
      document.addEventListener(visibilityChangeEvent, onVisibilityChange);
    })(window)

猜你喜欢

转载自www.cnblogs.com/anxiaoyu/p/10334992.html