check for online/offline events if fetching should be enabled

This commit is contained in:
Thomas Rupprecht 2019-08-28 21:50:30 +02:00
parent 5f3af74c07
commit 944b99d1ad
1 changed files with 25 additions and 3 deletions

View File

@ -57,7 +57,29 @@ const fetchNewData = () => {
});
};
fetchNewData();
setInterval(() => {
let intervalHandler = null;
const stopFetching = () => {
if (intervalHandler !== null) {
clearInterval(intervalHandler);
intervalHandler = null;
}
};
const startFetching = () => {
fetchNewData();
}, Config.refreshTimeout);
if (intervalHandler === null) {
intervalHandler = setInterval(() => {
fetchNewData();
}, Config.refreshTimeout);
}
};
window.addEventListener('offline', () => {
stopFetching();
});
window.addEventListener('online', () => {
startFetching();
});
if (window.navigator.onLine) {
startFetching();
}