diff --git a/background/index.js b/background/index.js index 51eeea8..ce35152 100644 --- a/background/index.js +++ b/background/index.js @@ -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); \ No newline at end of file + if (intervalHandler === null) { + intervalHandler = setInterval(() => { + fetchNewData(); + }, Config.refreshTimeout); + } +}; + +window.addEventListener('offline', () => { + stopFetching(); +}); +window.addEventListener('online', () => { + startFetching(); +}); +if (window.navigator.onLine) { + startFetching(); +}