fix: interval

This commit is contained in:
alexsparkes
2022-09-05 08:14:21 +01:00
parent 14c33eac1e
commit 68783429d4
5 changed files with 65 additions and 51 deletions

View File

@@ -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) {

View File

@@ -233,24 +233,26 @@ export default class QuoteSettings extends PureComponent {
'modals.main.settings.sections.background.interval.title',
)}
name="quotechange"
name2="quoteStartTime"
value2={Date.now()}
>
<option value="refresh">{variables.getMessage('tabname')}</option>
<option value="60000">
<option value={60000}>
{variables.getMessage('modals.main.settings.sections.background.interval.minute')}
</option>
<option value="1800000">
<option value={1800000}>
{variables.getMessage(
'modals.main.settings.sections.background.interval.half_hour',
)}
</option>
<option value="3600000">
<option value={3600000}>
{variables.getMessage('modals.main.settings.sections.background.interval.hour')}
</option>
<option value="86400000">
<option value={86400000}>
{variables.getMessage('modals.main.settings.sections.background.interval.day')}
</option>
<option value="604800000">{variables.getMessage('widgets.date.week')}</option>
<option value="2628000000">
<option value={604800000}>{variables.getMessage('widgets.date.week')}</option>
<option value={2628000000}>
{variables.getMessage('modals.main.settings.sections.background.interval.month')}
</option>
</Dropdown>

View File

@@ -74,8 +74,13 @@ export default class BackgroundSettings extends PureComponent {
<Dropdown
label={variables.getMessage('modals.main.settings.sections.background.interval.title')}
name="backgroundchange"
name2="backgroundStartTime"
value2={Date.now()}
>
<option value="refresh">{variables.getMessage('tabname')}</option>
<option value="10000">
10 secs
</option>
<option value="60000">
{variables.getMessage('modals.main.settings.sections.background.interval.minute')}
</option>

View File

@@ -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() {

View File

@@ -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() {