From 7613116bf3a08de657ef535c5502d866f8a1e7fb Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Sat, 15 May 2021 11:31:55 +0100 Subject: [PATCH] Only automatically update when page not being viewed --- views/index.html | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/views/index.html b/views/index.html index b614df8..81f2690 100644 --- a/views/index.html +++ b/views/index.html @@ -450,27 +450,39 @@ } }; + 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 setInterval(() => { - fetch(`/api/unread`) - .then(res => res.json()) - .then(items => { - if (!this.showRead) { - if (this.selectedItem && !items.some(i => i.ID == this.selectedItem)) { - items.unshift(this.items.find(i => i.ID == this.selectedItem)); - } + if (!inView) { + fetch(`/api/unread`) + .then(res => res.json()) + .then(items => { + if (!this.showRead) { + if (this.selectedItem && !items.some(i => i.ID == this.selectedItem)) { + items.unshift(this.items.find(i => i.ID == this.selectedItem)); + } - this.items = items; - } else { - for (let item of items) { - if (!this.items.some(i => i.ID == item.ID)) { - this.items.unshift(item); + this.items = items; + } else { + for (let item of items) { + if (!this.items.some(i => i.ID == item.ID)) { + this.items.unshift(item); + } } } - } - this.setPageTitle(); - }) + this.setPageTitle(); + }); + } }, 5 * 60 * 1000); document.addEventListener('keydown', this._keyListener.bind(this));