Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda