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

127
Views
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 answers
Answer question

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 Report

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 Report

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