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

92
Vistas
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 Respuestas
Responde la pregunta

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 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