From 68783429d4ccf77ced64bd312c4462f3a3d27e7e Mon Sep 17 00:00:00 2001 From: alexsparkes Date: Mon, 5 Sep 2022 08:14:21 +0100 Subject: [PATCH] fix: interval --- .../modals/main/settings/Dropdown.jsx | 1 + .../modals/main/settings/sections/Quote.jsx | 14 ++--- .../sections/background/Background.jsx | 5 ++ .../widgets/background/Background.jsx | 53 ++++++++++--------- src/components/widgets/quote/Quote.jsx | 43 ++++++++------- 5 files changed, 65 insertions(+), 51 deletions(-) diff --git a/src/components/modals/main/settings/Dropdown.jsx b/src/components/modals/main/settings/Dropdown.jsx index 4175608c..f444a508 100644 --- a/src/components/modals/main/settings/Dropdown.jsx +++ b/src/components/modals/main/settings/Dropdown.jsx @@ -29,6 +29,7 @@ export default class Dropdown extends PureComponent { if (!this.props.noSetting) { localStorage.setItem(this.props.name, value); + localStorage.setItem(this.props.name2, this.props.value2) } if (this.props.onChange) { diff --git a/src/components/modals/main/settings/sections/Quote.jsx b/src/components/modals/main/settings/sections/Quote.jsx index a3daaea1..bcbd182f 100644 --- a/src/components/modals/main/settings/sections/Quote.jsx +++ b/src/components/modals/main/settings/sections/Quote.jsx @@ -233,24 +233,26 @@ export default class QuoteSettings extends PureComponent { 'modals.main.settings.sections.background.interval.title', )} name="quotechange" + name2="quoteStartTime" + value2={Date.now()} > - - - - - - + diff --git a/src/components/modals/main/settings/sections/background/Background.jsx b/src/components/modals/main/settings/sections/background/Background.jsx index 62238020..14273ce0 100644 --- a/src/components/modals/main/settings/sections/background/Background.jsx +++ b/src/components/modals/main/settings/sections/background/Background.jsx @@ -74,8 +74,13 @@ export default class BackgroundSettings extends PureComponent { + diff --git a/src/components/widgets/background/Background.jsx b/src/components/widgets/background/Background.jsx index c70d829b..381032db 100644 --- a/src/components/widgets/background/Background.jsx +++ b/src/components/widgets/background/Background.jsx @@ -383,25 +383,31 @@ export default class Background extends PureComponent { return this.setState(JSON.parse(localStorage.getItem('welcomeImage'))); } - const interval = localStorage.getItem('backgroundchange'); - if (interval && interval !== 'refresh') { + if (localStorage.getItem('backgroundchange') === 'refresh') { + try { + document.getElementById('backgroundImage').classList.remove('fade-in'); + document.getElementsByClassName('photoInformation')[0].classList.remove('fade-in'); + } catch (e) { + // Disregard exception + } + this.getBackground(); + localStorage.setItem('backgroundStartTime', Date.now()); + } + + this.interval = setInterval(() => { + const targetTime = Number( + Number(localStorage.getItem('backgroundStartTime')) + + Number(localStorage.getItem('backgroundchange')), + ); + const currentTime = Number(Date.now()); const type = localStorage.getItem('backgroundType'); - if (type === 'api' || type === 'custom') { - Interval( - () => { - try { - document.getElementById('backgroundImage').classList.remove('fade-in'); - document.getElementsByClassName('photoInformation')[0].classList.remove('fade-in'); - } catch (e) { - // Disregard exception - } - this.getBackground(); - }, - Number(interval), - 'background', - ); - + if (currentTime >= targetTime) { + console.log('Is this true?') + this.getBackground(); + localStorage.setItem('backgroundStartTime', Date.now()); + } else { + console.log('Or this?') try { const current = JSON.parse(localStorage.getItem('currentBackground')); if (current.type !== type) { @@ -412,24 +418,18 @@ export default class Background extends PureComponent { this.setState(current); } else if (current.url.startsWith('http')) { this.setState(offlineBackground()); - } else { - if (offline === 'false') { - localStorage.removeItem('currentBackground'); - return this.getBackground(); - } - this.setState(current); } + this.setState(current); } catch (e) { this.setBackground(); } } - } else { - this.getBackground(); - } + }); } // only set once we've got the info componentDidUpdate() { + clearInterval(this.interval); if (this.state.video === true) { return; } @@ -439,6 +439,7 @@ export default class Background extends PureComponent { componentWillUnmount() { EventBus.off('refresh'); + clearInterval(this.interval); } render() { diff --git a/src/components/widgets/quote/Quote.jsx b/src/components/widgets/quote/Quote.jsx index f122ac24..2a98d563 100644 --- a/src/components/widgets/quote/Quote.jsx +++ b/src/components/widgets/quote/Quote.jsx @@ -359,32 +359,37 @@ export default class Quote extends PureComponent { } }); - const interval = localStorage.getItem('quotechange'); - if (interval && interval !== 'refresh' && localStorage.getItem('quoteType') === 'api') { - Interval( - () => { - this.setZoom(); - this.getQuote(); - }, - Number(interval), - 'quote', - ); - - try { - this.setState(JSON.parse(localStorage.getItem('currentQuote'))); - } catch (e) { - this.setZoom(); - this.getQuote(); - } - } else { - // don't bother with the checks if we're loading for the first time + if (localStorage.getItem('quotechange') === 'refresh') { this.setZoom(); this.getQuote(); + localStorage.setItem('quoteStartTime', Date.now()); } + + this.interval = setInterval(() => { + const targetTime = Number( + Number(localStorage.getItem('quoteStartTime')) + + Number(localStorage.getItem('quotechange')), + ); + const currentTime = Number(Date.now()); + if (currentTime >= targetTime) { + this.setZoom(); + this.getQuote(); + localStorage.setItem('quoteStartTime', Date.now()); + } else { + console.log(localStorage.getItem('quotechange')); + try { + this.setState(JSON.parse(localStorage.getItem('currentQuote'))); + } catch (e) { + this.setZoom(); + this.getQuote(); + } + } + }); } componentWillUnmount() { EventBus.off('refresh'); + clearInterval(this.interval); } render() {