问题
Actually we Play a notification sound when some update happens in database, so I am trying to reload page every 5 seconds when browser in minimized, this works fine in Firefox , but in Chrome this doesn't work.
Scenario:
Minimize the browser and leave the computer idle.
I have tried two methods:
Method 1:
<meta http-equiv="refresh" content="5;URL=http://example.com/" />
Method 2:
<script>
$(document).ready(function () {
setTimeout(function(){
window.location.reload(1);
}, 5000);
});
</script>
Any help would be great.
回答1:
The window blur and focus events can detect the view state of the window .
Example :
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);
Hope this helps.
来源:https://stackoverflow.com/questions/38376157/javascript-reload-page-every-5-seconds-when-browser-is-minimized