From acaf1da268036bb893125482faa8f4c0867fc0dc Mon Sep 17 00:00:00 2001 From: Henri Bergius Date: Wed, 7 Feb 2018 15:59:35 +0100 Subject: [PATCH] Only update after successful fetch to avoid spamming when MPD is unavailable --- elements/mpd.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/elements/mpd.js b/elements/mpd.js index ff9a839..c9ae47c 100644 --- a/elements/mpd.js +++ b/elements/mpd.js @@ -33,22 +33,17 @@ class MPD extends Component { return; } this.fetchData(); - if (!this.interval) { - return; - } - this.runInterval = window.setInterval(() => { - this.fetchData(); - }, this.interval * 1000); } disconnecting() { - if (this.runInterval) { - window.clearInterval(this.runInterval); - delete this.runInterval; + if (this.timeout) { + window.clearTimeout(this.timeout); + delete this.timeout; } } fetchData() { + delete this.timeout; const url = `https://c-beam.cbrp3.c-base.org/mpd/${this.mpd}/status/?_=${Date.now()}`; fetch(url, { headers: { @@ -58,6 +53,12 @@ class MPD extends Component { .then(data => data.json()) .then((data) => { this.data = data.content; + if (!this.interval) { + return; + } + this.timeout = window.setTimeout(() => { + this.fetchData(); + }, this.interval * 1000); }); }