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

163
Vistas
How to set loop with pause in between each log

How could I make a loop that pauses for a set amount of time before repeating? I tried using setTimeout inside of a while loop but that didn't work.

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

0

JavaScript is monothreaded, so blocking is to be avoided, that is why so much effort was pourred into the language to standardise Promise and add async / await

This allow to pause code execution without blocking, here how you do it:

// Only available in Node v15+
import { setTimeout as pause } from 'timers/promises'

// if not in node:
const pause = delay => new Promise(s => setTimeout(s, delay))

async function sayHelloEverySeconds() {
  while (true) {
    await pause(1000) // 1 sec
    console.log('hello')
  }
}

sayHelloEverySeconds()

But for this exact function, it would be simpler to use setInterval directly:

setInterval(() => console.log('hello'), 1000)

this will execute the function every seconds

about 4 years ago · Juan Pablo Isaza Denunciar

0

You don't need to use async/await functions or promises or a while loop. Call a function and have setTimeout call the function again after a set amount of time.

function loop(count = 0) {
  console.log(count);
  setTimeout(loop, 1000, ++count);
}

loop();

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