39 lines
902 B
JavaScript
39 lines
902 B
JavaScript
|
let fetchType;
|
||
|
let fetchInterval;
|
||
|
let fetchURL;
|
||
|
|
||
|
|
||
|
onmessage = function(e) {
|
||
|
let msgType = e.data.msgType;
|
||
|
|
||
|
fetchType = e.data.fetchType;
|
||
|
fetchURL = e.data.fetchURL;
|
||
|
fetchInterval = 1000 * e.data.fetchInterval;
|
||
|
|
||
|
console.log("Configiuration Message:");
|
||
|
console.log(' Fetch type: ' + fetchType);
|
||
|
console.log(' Using fetch url: ' + fetchURL);
|
||
|
console.log(' Fetching data each [ms]: ' + fetchInterval);
|
||
|
};
|
||
|
|
||
|
|
||
|
function doFetch() {
|
||
|
let now = new Date();
|
||
|
|
||
|
if (fetchURL) {
|
||
|
fetch(fetchURL).then(
|
||
|
b => b.json()).then(
|
||
|
j => postMessage({msgType: fetchType,
|
||
|
json: j,
|
||
|
timestamp: now}))
|
||
|
.catch(e => console.log(e.message));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
// Periodic resource fetching
|
||
|
(function periodicFetch() {
|
||
|
doFetch();
|
||
|
setTimeout(periodicFetch, fetchInterval);
|
||
|
})();
|