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

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

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 Denunciar

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 Denunciar

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