I want a use Fetch()
or Jquery GetJson() to fetch some json data (json file) every 5 seconds independently.
I want another function to use that data and display an array of names choosing 8 at a time (out of 100 or so names) in a loop. (still to be implemented)
How do I go about waiting for the data? I know I need to wait for the data to return asynchronously.
var data = null;
var dataPath = "../data/GolfData.json";
$(document).ready(function () {
console.log('working...');
DisplayData(data);
});
/* Fetch Data
------------------------------------------ */
function FetchData() {
fetch(dataPath)
.then(function (response) {
return response.json();
})
.then(function (json) {
data = json;
console.log(data)
return data;
})
.catch(function (error) {
setInterval(FetchData, 5000);
console.log(error);
})
}
FetchData();
setInterval(FetchData, 5000);
function DisplayData(data) {
console.log('data ', data);
}
Console: data null