实际上我们在数据库中发生某些更新时播放通知声音,因此我尝试在浏览器最小化时每5秒重新加载页面,这在Firefox中运行良好,但在Chrome中这不起作用。
情境:
最小化浏览器并让计算机空闲。
我尝试了两种方法:
方法1:
<meta http-equiv="refresh" content="5;URL=http://example.com/" />
方法2:
<script>
$(document).ready(function () {
setTimeout(function(){
window.location.reload(1);
}, 5000);
});
</script>
任何帮助都会很棒。
答案 0 :(得分:1)
窗口模糊和焦点事件可以检测到窗口的视图状态。
示例:
var timer = null;
//when the window is minimized or when user is in different tab .
window.addEventListener('blur', function(){
timer = setInterval(function(){
window.location.reload(1);
},5000)
}, false);
//when user is back to window
window.addEventListener('focus', function(){
//stop the page reload once
if(timer != null){
clearInterval(timer);
}
}, false);
希望这有帮助。