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

124
Visualizações
infinitetly loop array with setTimeout javascript

I have an array of strings that I'm passing to a for loop with a setTimeout inside. I've tried to for(; ;) and while(true) the function but this just causes the browser to crash.

I've also tried adding an if statement inside the function

if (x == links.length) { loopUrls() }

Which works kind of but skips the entire interval section of the function, cycling the entire array infinitely with no wait.

        function loopUrls() {
            for (var x = 0, ln = links.length; x < ln; x++) {
                setTimeout(function (y) {
                    document.getElementById('ifURLS').src = links[y];
                },x * 500,x);
            }
    };

    loopUrls();

The aim is to have a list of urls that infinitely assigned to the iframe to show reports on a sreen. (the time interval is just small for testing purposes)

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

0

If I understand correctly, you want for every link to have some function that is being executed every X milliseconds.

In order to achieve this I think it's better to use setInterval.

You can do something like:

links.forEach((link) => {
  setInterval(() => {
    //do something every X milliseconds
  }, X)
})
about 4 years ago · Juan Pablo Isaza Relatório

0

a way to do that :

// const links = [ ... ]

const myImg = document.queryselector('#ifURLS')

function LoadImg()
  {
  let 
    indx = 0
  , let refIntv = 
      setInterval( ()=>
        {
        myImg.src = links[indx++]
      
        if (indx >= links.length)
          clearInterval( refIntv ) 
        }
        , 5000 )  // 5s delay
  }
about 4 years ago · Juan Pablo Isaza Relatório

0

There is no need to have multiple timeouts. You can achieve this using setInterval which executes a function every x amount of time. Then in that funciton you keep a state of the current url so you can show the next time the function is run

// creates a function that iterates over the array and every time its called returns the next element
function createIterator(array){
    let sourceArray = [...array];
    // starts at the end so it resets automatically
  let topIndex = sourceArray.length;
    let currentIndex = topIndex - 1;
    return function (){
        currentIndex++;
        // if the limit is reached, its reseated
        if (currentIndex === topIndex) {
            currentIndex = 0;
        }
        return sourceArray[currentIndex];
    }
}
// your urls
let urls = ["a", "b", "c"];
// uses the function to create the iterator
let iterator = createIterator(urls);

setInterval(function() {
    let currentUrl = iterator();
    document.getElementById('ifURLS').src = currentUrl;
},1000);

No need for loops, as the iterator handles seamlessly

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