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

221
Vistas
Trying to run a loop for exactly one second, but the loop runs infinitely. why?

i am changing the value of 'aa' after 1 second, but the loop continues to execute.

let aa = true;

setTimeout(function () {
  aa = false;
}, 1000);

for (; aa; ) {
  console.log('aaa');
}
about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

setTimeout is asynchronous, so it will execute only after the current code execution of for loop completed. In your case it will go infinite.

Once for loop started execution, it will stay in the call stack until finished. Since you didn't give any condition there it won't stop

about 4 years ago · Santiago Gelvez Denunciar

0

Your approach doesn’t work for reasons explained by others. You can use something like this instead:

var started = Date.now();
while(Date.now() - started < 1000) {
  console.log("aaa");
};

about 4 years ago · Santiago Gelvez 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