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)

 

posted @ 2019-01-29 17:26  安筱雨  阅读(308)  评论(0编辑  收藏  举报