当某人不在站点屏幕上时如何重新加载窗口。但是在屏幕上不应重新加载窗口

时间:2020-06-04 04:46:20

标签: javascript json

我正在使用此代码重新加载窗口。我希望当有人不在屏幕上时,应该重新加载。但是,如果有人不在屏幕上,则应一次又一次地自动重新加载。

<html>
<header>
    <!-- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.11/vue.min.js"></script> -->
</header>
<body onload="AutoRefresh(1000)">
    <p id="req">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,</p>

<p>
<script src="index.js">
     function AutoRefresh( t ) {
               setTimeout("location.reload(true);", t);
      }
</script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这将起作用:

var hidden, visibilityChange; 
if (typeof document.hidden !== "undefined") {
  hidden = "hidden";
  visibilityChange = "visibilitychange";
} else if (typeof document.msHidden !== "undefined") {
  hidden = "msHidden";
  visibilityChange = "msvisibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
  hidden = "webkitHidden";
  visibilityChange = "webkitvisibilitychange";
}
 

function handleVisibilityChange() {
  if (document[hidden]) {
    // do something when inactive
  } else {
    // do something when active
  }
}

document.addEventListener(visibilityChange, handleVisibilityChange, false);