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

123
Views
matriz de bucle infinito con setTimeout javascript

Tengo una matriz de cadenas que paso a un bucle for con un setTimeout dentro. Intenté for(; ;) y while(true) la función, pero esto solo hace que el navegador se bloquee.

También intenté agregar una declaración if dentro de la función

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

Lo que funciona pero se salta toda la sección de intervalo de la función, ciclando toda la matriz infinitamente sin esperar.

 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();

El objetivo es tener una lista de URL que se asignan infinitamente al iframe para mostrar informes en una pantalla. (el intervalo de tiempo es pequeño para fines de prueba)

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Si entiendo correctamente, desea que cada enlace tenga alguna función que se ejecute cada X milisegundos.

Para lograr esto, creo que es mejor usar setInterval .

Puedes hacer algo como:

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

0

una manera de hacer eso:

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

0

No es necesario tener múltiples tiempos de espera. Puede lograr esto usando setInterval que ejecuta una función cada x cantidad de tiempo. Luego, en esa función, mantiene un estado de la URL actual para que pueda mostrar la próxima vez que se ejecute la función

 // 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 hay necesidad de bucles, ya que el iterador se maneja sin problemas

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!