mardi 26 janvier 2021

Trying to load a random link out of array

I know this is a very noob question, looked up a couple of examples but me being a beginner I'm having trouble implementing it in my code.

I think the title is pretty clear cut, I would like that the loop starts with a random item, and continues picking random items out of the array.

var bgImageArray = [
"link1" , "link2" , "link3"
]

base = "https://lh3.googleusercontent.com/pw/";
bgImageArray.forEach(function(img){
    new Image().src = base + img; 
});

function backgroundSequence() {
  window.clearTimeout();
  var k = 0;
  for (i = 0; i < bgImageArray.length; i++) {
    setTimeout(function(){ 
      document.getElementById('animated-bg').style.background = "url(" + base + bgImageArray[k] + ") no-repeat center center";
    if ((k + 1) === bgImageArray.length) { setTimeout(function() { backgroundSequence() }, (60000 / tempo.value))} else { k++; }      
    }, (60000 / tempo.value) * i) 
  }
}
``



Aucun commentaire:

Enregistrer un commentaire