问题
(sorry for my bad english)
I'm developing a basic chrome extension that make some changes to a site. It is all working fine but I can't get this to work: When the user is not on the page (is on another tab or minimize the browser) i need to refresh the page every 5 minutes and show a notification if something changed. My problem is to detect if the user is "not on the page"... I tried to do this but didn't worked:
window.addEventListener('focus', function() {
isVisible = true;
});
window.addEventListener('blur', function() {
isVisible = false;
});
The "blur" for some reason do not get called (or is called only sometimes)... there is another way to do this?
回答1:
Use the Page Visibility API. Something like the following should work.
function handleVisibilityChange() {
if (document.webkitHidden) {
// refresh every 5 minutes
} else {
// stop refreshing every 5 minutes
}
}
document.addEventListener("webkitvisibilitychange", handleVisibilityChange, false);
来源:https://stackoverflow.com/questions/8705200/chrome-extension-refresh-page-when-is-not-visible