Helloworld/test.js

57 lines
1.5 KiB
JavaScript

let intervalIds = [];
let listToRandom = [0, 1, 2, 3, 4, 5]
let countPerso = 0;
fetch('http://localhost:3000/code')
.then(function(response) {
return response.json();
})
.then(function(data) {
let notSoRandomList = []
function runIntervals() {
intervalIds.forEach(clearInterval);
intervalIds = [];
let timeOuter = 3000;
let timeInner = 500;
let countAnim = 1;
if(countPerso === 6){
countPerso = 0
}
if(countPerso === 0){
notSoRandomList = notSoRandomPerso()
}
let perso = notSoRandomList[countPerso]
let length = data[perso].length
if(length*timeInner > timeOuter){
timeOuter = length*timeInner + timeInner
}
countPerso = countPerso + 1;
let imgElement = document.getElementById(perso);
if (imgElement){
intervalIds.push(setInterval(function() {
let anim = countAnim ++;
let check = (data[perso][anim]);
if(check === undefined){
anim = 0
}
let imgElement = document.getElementById(perso);
imgElement.src = (data[perso][anim]);
}, timeInner));
}
if(imgElement.src != data[perso][0])
{
setTimeout(function(){
runIntervals()
}, timeOuter);
}
}
function notSoRandomPerso(){
for (let i = listToRandom.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[listToRandom[i], listToRandom[j]] = [listToRandom[j], listToRandom[i]];
}
console.log(listToRandom)
return listToRandom
}
runIntervals();
});