I am trying to loop over a json array with the fetch()
function and display 8 items at a time for 8 seconds and repeat itself but javascript is going crazy and running through the array after a while.
The first iteration is fine.
Any help much appreciated.
Not sure whats going wrong.
$(document).ready(function () {
console.log('working...');
FetchData();
});
function FetchData() {
fetch(dataPath)
.then(function (response) {
return response.json();
})
.then(function (json) {
data = json;
clearInterval();
DisplayData(data);
})
.catch(function (error) {
console.log(error);
setInterval(FetchData, 5000);
})
}
function DisplayData(data) {
let golferDisplayData = data.Golfers;
var indexOffset = 0;
$containerContentSection.html(templateMainContent(golferDisplayData.slice(indexOffset, indexOffset + 8)));
indexOffset += 7;
setInterval(() => {
console.log(indexOffset);
// console.log(indexOffset, golferDisplayData.length);
// console.log(golferDisplayData.slice(indexOffset, indexOffset + 8));
$containerContentSection.html(templateMainContent(golferDisplayData.slice(indexOffset, indexOffset + 8)));
indexOffset += 7;
if(indexOffset >= golferDisplayData.length) {
indexOffset = 0;
FetchData();
}
}, 8000);
}
I am trying to loop over a json array with the fetch()
and display 8 items every 8 seconds at a time and repeat itself, After it completes one rotation it should fetch the json file again.