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

180
Vistas
How to output 10 numbers in JavaScript using for loop one by one?

I am interested in outputting 10 numbers printed one by one using JavaScript.

The HTML code I used to output 10 numbers using JavaScript is:

let z = "";
for (y = 0; y < 11; y = y + 1){
  x = y + 10;
  z = z + " Number " + x + " is printed. <br>";
}

setInterval(function() {
  document.getElementById("demo").innerHTML = z
}, 1000)
<!DOCTYPE html>
<html>

<body>
  <h2>How to use For Loop in JavaScript</h2>
  <p>Output 10 numbers using for loop in Javascript.</p>
  <p id="demo"></p>
</body>

</html>

The problem is that the output is printed all at once after one second instead of printing the output one by one after one second.

How do we modify the code so that the individual output will be printed after one second?

Thank you!

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

0

Put your actual logic inside the interval

let z = "";
let y = 0;

const interval = setInterval(function() {
  if (y >= 11) return clearInterval(interval);
  y++;
  const x = y + 10;
  z = z + " Number " + x + " is printed. <br>";
  document.getElementById("demo").innerHTML = z
}, 1000)
<!DOCTYPE html>
<html>

<body>
  <h2>How to use For Loop in JavaScript</h2>
  <p>Output 10 numbers using for loop in Javascript.</p>
  <p id="demo"></p>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Use a recursive function with setTimeout instead of setInterval.

function printDelay(max, iteration){
    const newLine = `Number ${iteration + 10} is printed.<br>`;

    const demo = document.getElementById("demo");
    demo.innerHTML = demo.innerHTML + newLine;
    if(iteration < max){
      setTimeout(()=>printDelay(max, iteration + 1),1000);
    }
}

printDelay(10,1);
<!DOCTYPE html>
<html>

<body>
  <h2>How to use For Loop in JavaScript</h2>
  <p>Output 10 numbers using for loop in Javascript.</p>
  <p id="demo"></p>
</body>

</html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

// init value
var i = 1; 
// func decleartion
function PrintOneByOne() { 
  // setTimeout
  // print for every 3 sec ~ 3000 ms
  // 
  setTimeout(function() {
    z = " Number " + i + " is printed. <br>";
    // append to element
    document.getElementById("demo").innerHTML += z
    i++; 
    if (i <= 10) {
      PrintOneByOne(); 
    } 
  }, 3000)
}

// call func
PrintOneByOne();
<!DOCTYPE html>
<html>

<body>
  <h2>How to use For Loop in JavaScript</h2>
  <p>Output 10 numbers using for loop in Javascript.</p>
  <p id="demo"></p>
</body>

</html>

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