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

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

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