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

266
Vistas
How to print number after a delay in JS

It's beginner's question. I'm trying to understand setTimeout. I want to print each number after a delay of 2 seconds. However, I can only delay the whole function, not the numbers. Can anyone tell me how to do it?

function print() {
  for (let i = 10; i >= 0; i--) {
    console.log(i);
  }
}

setTimeout(print, 2000);

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

0

You need to use the setTimeout inside the print method (inside the for), and give a different delay for each iteration.

function print() {
  for (let i = 10; i >= 1; i--) {
    setTimeout(() => {
      console.log(i);
    }, 2000 * (10 - i));
  }
}

print();

about 4 years ago · Juan Pablo Isaza Denunciar

0

Another approach is using setInterval. It is more natural for this task then setTImeout.

let i = 10;
const interval = setInterval(() => {
  console.log(i);
  i--;
  if(i === 0){
    clearInterval(interval);
  }
}, 2000);

about 4 years ago · Juan Pablo Isaza Denunciar

0

setTimeout here will delay all the function and if I get what you want to do is to print each number in 2s delay. ill recommend using Promises and async function. here is a solution with only setTimeout

function print(i){
  console.log(i)
  if(i>0){
    setTimeout(()=>{print(i-1)},2000)
  }
}
print(10)
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