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

207
Visualizações
setInterval to change style of set of elements one by one in javascript without jquery

I have this piece of a code where I take data out of JSON and put them into four sections.

Having those four sections, I want to add a border to one section for 2 seconds, then remove and add the border to another section. And so on. Once the border is at the last section, the whole process should start again.

From what I read in similar threads, I should use setInterval and add/remove class to each element via looping, but not sure how to write it. Anyone could help?

function completeAd(obj) {
  const offers = obj['offers'];

  for (let i = 0; i < 4; i++) {
    let rand = Math.floor((Math.random() * offers.length));

    const singleAd = document.createElement('section');
    singleAd.className = "adSection"
    const price = document.createElement('p');
    const imgURL = document.createElement('img')

    imgURL.src = "http:" + offers[rand].imgURL
    price.textContent = offers[rand].price + " " + offers[rand].currency;

    singleAd.appendChild(price);
    singleAd.appendChild(imgURL);

    adSection.appendChild(singleAd);

  }
}

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes you need to add and remove a class in an interval

https://jsfiddle.net/mplungjan/pt68Lqoe/

const obj = {
offers : [ 
  { price:200, currency:"$", imgURL: "https://picsum.photos/200/200" },
  { price:300, currency:"$", imgURL: "https://picsum.photos/300/200" },
  { price:400, currency:"$", imgURL: "https://picsum.photos/400/200" },
  { price:500, currency:"$", imgURL: "https://picsum.photos/500/200" },
  { price:600, currency:"$", imgURL: "https://picsum.photos/600/200" }
]}


const fy = (a, b, c, d) => { c = a.length; while (c) b = Math.random() * (--c + 1) | 0, d = a[c], a[c] = a[b], a[b] = d}; // shuffle

fy(obj.offers); // shuffle them

window.addEventListener("load", () => {
  document.getElementById("adSections").innerHTML = obj.offers.slice(0, 4) // take the first 4
    .map(offer => `<section class="adSection"><p>${offer.price}${offer.currency}</p><img src="${offer.imgURL}" />`).join('');

  const sections = document.querySelectorAll(".adSection");
  let cnt = sections.length - 1;
  setInterval(function() {
    sections[cnt].classList.remove("active");
    cnt++;
    if (cnt >= sections.length) cnt = 0;
    sections[cnt].classList.add("active")
  }, 3000)

})
section {
  display: block;
  border: 1px solid white;
  width: 500px
}

.active {
  border: 1px solid black
}

img {
  height: 200px
}
<div id="adSections">


</div>

about 4 years ago · Juan Pablo Isaza 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