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

161
Vistas
call a function for n times with 2 second delay

Im running a code where I must create a function and call it for 3 times with 2 seconds delay between each.I am having trouble calling it more than once. how can i solve this?I use javascript. i create myfunc() to log hello in console every 2 seconds for 3 times.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

const myFunc = () => console.log("hello");

const runFunc = async (func, times, delay) => {
  for (let i = 0; i < times; i++) {
    func();
    if (i + 1 < times) await new Promise(res => setTimeout(res, delay));
  }
}

runFunc(myFunc, 3, 2 * 1000);

or this

const myFunc = () => console.log("hello");

const runFunc = async (func, times, delay) => {
  if (times >= 1) func();
  times--;
  if (times >= 1) {
    const interval = setInterval(() => {
      times--;
      if (times <= 0) clearInterval(interval);
      func();
    }, delay);
  }
}

runFunc(myFunc, 3, 2 * 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