Only automatically update when page not being viewed

This commit is contained in:
Marcus Noble 2021-05-15 11:31:55 +01:00
parent a21dd6b74f
commit 7613116bf3

View File

@ -450,8 +450,19 @@
} }
}; };
let inView = true;
window.onfocus = window.onblur = window.onpageshow = window.onpagehide = function (e) {
if ({focus:1, pageshow:1}[e.type]) {
if (inView) return;
inView = true;
} else if (inView) {
inView = false;
}
};
// Fetch updates every 5 minutes // Fetch updates every 5 minutes
setInterval(() => { setInterval(() => {
if (!inView) {
fetch(`/api/unread`) fetch(`/api/unread`)
.then(res => res.json()) .then(res => res.json())
.then(items => { .then(items => {
@ -470,7 +481,8 @@
} }
this.setPageTitle(); this.setPageTitle();
}) });
}
}, 5 * 60 * 1000); }, 5 * 60 * 1000);
document.addEventListener('keydown', this._keyListener.bind(this)); document.addEventListener('keydown', this._keyListener.bind(this));