Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
Slow down image cycle rotation to eventual random stop

I have a list of images that the code cycles through and displays indefinitely.

I want it to slow down and stop on a random image in the list.

Can anyone help me with this?

This is what I have so far:

<div id="imgSelect" class="imgSelect">
    <div id="img" class="img">
        <img id="imgDisplay" class="imgInside" imgalt="rArrowback">
    </div>
    <div id="select" class="select">
        <button id="selectBtn" type="button" class="btnSelect">GO!</button>
    </div>
</div>

The funtion:

var imgArray = new Array();
for (var i = 0; i < 60; i++) {
    imgArray[i] = './imgs/' + i + '.png';
}
document.getElementById("imgDisplay").style.backgroundImage = "url('qm.png')"

function selectImg() {
    document.getElementById("imgDisplay").style.backgroundImage = 'none'
    var rotator = document.getElementById("imgDisplay");
    var delayInSeconds = 1;
    var num = 0;
    var changeImage = function () {
        var len = imgArray.length;
        rotator.src = imgArray[num++];
        if (num == len) {
            num = 0;
        }
    };
    setInterval(changeImage, delayInSeconds * 100);

    // for (var i = 0; i < 60; i++) {
    //     document.getElementById("imgDisplay").style.backgroundImage = "url('" + imgArray[i] + "')"
    // }
}

This is the result:

result

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Use the random function offered by Javascript.

Right after the timer put rotator.src = imgArray[Math.floor(Math.random() * imgArray.length)];

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda