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

140
Views
cómo no inundar la página html con el html interno

tengo este codigo:

 function addInDiv() { let div = document.getElementById("divName"); div.innerHTML += '<p>' + "test" + '</p>' } setInterval(addInDiv, 1000);

este código hace

 <p>test</p> <p>test</p> <p>test</p> <p>test</p> <p>test</p>

... infinitamente e inundar la CPU de la computadora.

Me gustaría que "divName" solo contenga las últimas 5 líneas que ha agregado div.innerHTML

Entonces, ¿cómo puedo mostrar las últimas 5 líneas mostradas para no inundar la página html y la CPU de la PC?

Gracias

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

0

Almacene la cantidad de elementos agregados en una variable, increméntela cada vez que agregue y verifique si se agregaron 5 veces o no. Si es así, cancele el intervalo.

 var appended = 0; var interval function addInDiv() { if (++appended == 6) { clearInterval(interval) } let div = document.getElementById("divName"); div.innerHTML += '<p>' + "test" + '</p>' } interval = setInterval(addInDiv, 1000);
 <div id="divName"></div>

about 4 years ago · Juan Pablo Isaza Report

0

Con jQuery:

 var counter = 0; function addInDiv() { counter++ if(counter <= 5) { $('#divName').append('<p>' + "test" + '</p>'); } } setInterval(addInDiv, 1000);
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!