Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda