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

211
Views
Change each text after 4 second and each image after 2 second

Basically first two images(image1, image2) from array depend on first string(String1) from array. and other two images on String2, so i want string1 change after 4 second but in this time two images being change that relates string1 and so on for string2 and their images.

<script>
var text = ["String1", "String2"];
var backgroundImg=["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg"]
var counter = 0;
var currentPos = 0;
var elem = document.getElementById("animated-text");
var elembg = document.getElementById("animated-background");
var inst = setInterval(change, 4000);
var bginst = setInterval(changeImage, 2000);

function change() {
  elem.innerHTML = text[counter];
   
  counter++;
  if (counter >= text.length) {
    counter = 0;
    // clearInterval(inst); // uncomment this if you want to stop refreshing after one cycle
  }
}

function changeImage() {   
        if (++currentPos >= backgroundImg.length){
        currentPos = 0;
        }
        elembg.style.backgroundImage = "url('"+backgroundImg[currentPos]+"')";
}  
</script>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Please check these codes.

<script>
        var text = ["String1", "String2"];
        var backgroundImg = ["image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg"]
        
        var elem = document.getElementById( "animated-text" );
        var elembg = document.getElementById( "animated-background" );
        
        var inst = setInterval( change, 4000 );
        var bginst = setInterval( changeImage, 2000 );
        
        var text_index = 0;
        var text_total = text.length - 1;
        function change() {
            elem.innerHTML = text[text_index];
            
            if ( text_index == text_total ) {
                text_index = 0;
            } else {
                text_index ++;
            }
        }
        
        var bd_index = 0;
        var bd_total = backgroundImg.length - 1;
        function changeImage() {
            elembg.style.backgroundImage = "url('" + backgroundImg[bd_index] + "')";
            
            if ( bd_index == bd_total ) {
                bd_index = 0;
            } else {
                bd_index ++;
            }
        }
    </script>
about 4 years ago · Santiago Trujillo 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!