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

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

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 Denunciar

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