diff --git a/server/scripts/modules/currentweather.mjs b/server/scripts/modules/currentweather.mjs index 12a9fab..9ee1b3f 100644 --- a/server/scripts/modules/currentweather.mjs +++ b/server/scripts/modules/currentweather.mjs @@ -130,6 +130,8 @@ class CurrentWeather extends WeatherDisplay { // make data available outside this class // promise allows for data to be requested before it is available async getCurrentWeather(stillWaiting) { + // an external caller has requested data, set up auto reload + this.setAutoReload(); if (stillWaiting) this.stillWaitingCallbacks.push(stillWaiting); return new Promise((resolve) => { if (this.data) resolve(this.data); diff --git a/server/scripts/modules/hourly.mjs b/server/scripts/modules/hourly.mjs index 8f237d8..1927350 100644 --- a/server/scripts/modules/hourly.mjs +++ b/server/scripts/modules/hourly.mjs @@ -139,6 +139,8 @@ class Hourly extends WeatherDisplay { // promise allows for data to be requested before it is available async getCurrentData(stillWaiting) { if (stillWaiting) this.stillWaitingCallbacks.push(stillWaiting); + // an external caller has requested data, set up auto reload + this.setAutoReload(); return new Promise((resolve) => { if (this.data) resolve(this.data); // data not available, put it into the data callback queue diff --git a/server/scripts/modules/weatherdisplay.mjs b/server/scripts/modules/weatherdisplay.mjs index 31b8913..d9e3a07 100644 --- a/server/scripts/modules/weatherdisplay.mjs +++ b/server/scripts/modules/weatherdisplay.mjs @@ -134,10 +134,9 @@ class WeatherDisplay { // refresh doesn't delete existing data, and is reused if the silent refresh fails if (!refresh) { this.data = undefined; + // clear any refresh timers + this.clearAutoReload(); } - // clear any refresh timers - clearTimeout(this.autoRefreshHandle); - this.autoRefreshHandle = null; // store weatherParameters locally in case we need them later if (weatherParameters) this.weatherParameters = weatherParameters; @@ -150,8 +149,8 @@ class WeatherDisplay { return false; } - // set up auto reload - this.autoRefreshHandle = setTimeout(() => this.getData(false, true), settings.refreshTime.value); + // set up auto reload if necessary + this.setAutoReload(); // recalculate navigation timing (in case it was modified in the constructor) this.calcNavTiming(); @@ -435,6 +434,15 @@ class WeatherDisplay { this.stillWaitingCallbacks.forEach((callback) => callback()); this.stillWaitingCallbacks = []; } + + clearAutoReload() { + clearInterval(this.autoRefreshHandle); + this.autoRefreshHandle = null; + } + + setAutoReload() { + this.autoRefreshHandle = this.autoRefreshHandle ?? setInterval(() => this.getData(false, true), settings.refreshTime.value); + } } export default WeatherDisplay;