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

98
Views
JavaScript Loop only works once
var block = [];

setInterval(function() {

block.push(document.createElement("div"));
for (let i = 0; i < block.length; i++) {

I made a class in css that has some stylings

block[i].setAttribute("class", "block");
document.body.appendChild(block[i]);

}}, 1000);
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

var block = [];

setInterval(function() {
  block.push(document.createElement("div"));

  for (let i = 0; i < block.length; i++) {
    block[i].setAttribute("class", "block");
    document.body.appendChild(block[i]);
  }}, 1000);

The reason why it only runs once: You only push one thing to the block array and on the for-loop, the block.length is 1 (because has only one item in it), which is true. Thus, it only gets run once.

According to your question, it sounds like you want to have a function execute a for-loop to update CSS styling.

Advice: Just push all the item or div you wanna update to the block before the for-loop.

about 4 years ago · Santiago Trujillo 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!