Helloworld/test.js

66 lines
1.7 KiB
JavaScript

let intervalIds = [];
let listToRandom = [0, 1, 2, 3, 4, 5]
let countPerso = 0;
console.log('ouaip')
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 === 5){
countPerso = 0
}
if(countPerso === 0){
notSoRandomList = notSoRandomPerso()
}
let perso = notSoRandomList[countPerso]
countPerso = countPerso + 1;
// let minPerso = 0;
// let maxPerso = 5;
// let perso = Math.floor(Math.random() * (maxPerso - minPerso + 1) + minPerso);
let length = data[perso].length
if(length*timeInner > timeOuter){
timeOuter = length*timeInner + timeInner
}
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
}
function stopIntervals() {
intervalIds.forEach(clearInterval);
intervalIds = [];
}
runIntervals();
});