Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

149
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!