Dear friends, I have made (with a help of You) a random image picker choosing 4 images from an image collection. The idea of this little project is that there are not duplicated images in a row - so there can be only ABCD not ABBC. The second condition is that the set of images changes after some time eg every 5 seconds.
The project can be used for example to present a list of clients on a webpage. Of course feel free to use it.
BUT maybe there is a better way to write it, so please feel free to comment it or to correct it. I personally wanted to make it with the loop and if statement but I had some problems with “if” the images kept repeating in a row like ABBC.
and the code:
var clients = [
{
logo: 'https://cdn.glitch.com/9e10365b-c704-4f66-b12a-f002faf03af3%2FMbank-logo.jpg?1557398130183'
},
{
logo: "https://cdn.glitch.com/9e10365b-c704-4f66-b12a-f002faf03af3%2Fnetia-logo.png?1557398130339"
},
{
logo: "https://cdn.glitch.com/9e10365b-c704-4f66-b12a-f002faf03af3%2Ffines_logo.jpg?1557398130199"
},
{
logo: "https://cdn.glitch.com/9e10365b-c704-4f66-b12a-f002faf03af3%2Fsevenet_logo.jpg?1557398130497"
},
{
logo: "https://cdn.glitch.com/9e10365b-c704-4f66-b12a-f002faf03af3%2FMcDonalds_logo.png?1557398130570"
}
];
function repeatImage(){
let result = clients.slice(0, 4).map(function () {
return this.splice(Math.floor(Math.random() * this.length), 1)[0];
}, clients.slice());
var firstOnScreen = result[0];
document.getElementById("firstSlideImage").src = firstOnScreen.logo;
var secondOnScreen = result[1];
document.getElementById("secondSlideImage").src = secondOnScreen.logo;
var thirdOnScreen = result[2];
document.getElementById("thirdSlideImage").src = thirdOnScreen.logo;
var fourthOnScreen = result[3];
document.getElementById("fourthSlideImage").src = fourthOnScreen.logo;
};
setInterval( repeatImage, 2000);
window.onload = function() {
repeatImage()
};
regards hubert