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

270
Views
Loop iteration with time interval before passing value to included function

I'm trying to figure out how to set time out for function inside the loop iteration in Ionic TypeScript application.

setInterval makes equal time interval with calling the function in endless repetition:

   setInterval(() => {
      this.myFunc1(val);
   }, 800);

setTimeout gives required result if listed sequentially:

   setTimeout(() => {
      this.myFunc1(val);
   }, 800); 

   setTimeout(() => {
      this.myFunc1(val);
   }, 1200); 

but how to loop with time interval trough the updated list and wait while pass second value val to the function, or call myFunc1 when it will be finished in previous iteration:

 async myFunc2() {
    for (let val of this.myValueList) {
        /// wait for 5 sec or wait for finishing process, then pass value calling function:  
        this.myFunc1(val);          
    }
  }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Simplest solution for waiting approximately 5 seconds:

const wait = t => new Promise(r => setTimeout(r, t));

and then in your code you can just do:

async myFunc2() {
    for (let val of this.myValueList) {
        await wait(5000);
        this.myFunc1(val);
    }
}

If myFunc1 is async, and you just want to wait for it to finish executing before continuing the loop, then you would just do:

async myFunc2() {
    for (let val of this.myValueList) {
        await this.myFunc1(val);
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

setInterval is the correct choice here. What's missing is that you need to clear the interval. setInterval returns an id that you can pass to clearInterval which stops the iteration.

Here I'm passing data to console.log, waiting a second, then repeating till done.

const myValueList = [5,6,7,8,9,10];

let i = 0;
const id = setInterval(() => {
  console.log(myValueList[i++]);
  if (i === myValueList.length) {
    clearInterval(id);
    console.log("done!");
  }
}, 1000);

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!