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

217
Visualizações
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 Respostas
Responde à pergunta

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 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