Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

184
Views
¿Cómo generar 10 números en JavaScript usando for loop uno por uno?

Estoy interesado en generar 10 números impresos uno por uno usando JavaScript.

El código HTML que usé para generar 10 números usando JavaScript es:

 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>

El problema es que la salida se imprime de una vez después de un segundo en lugar de imprimir la salida una por una después de un segundo.

¿Cómo modificamos el código para que la salida individual se imprima después de un segundo?

¡Gracias!

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Pon tu lógica real dentro del intervalo

 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 Report

0

Use una función recursiva con setTimeout en lugar de 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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!