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

200
Visualizações
Hide HTML element based on CSS class after specified duration

I would like to unhide all elements inside certain class on a webpage according to a chosen time written in the class.

For example:

  • class=".unhide15" should unhide the element after 15 seconds.
  • class=".unhide123" should unhide the element after 123 seconds.

And so on..

I arrived to this code below (it works), but I still can't chose any time I want.

<style>
    .unhide15, .unhide30, .unhide60{
      display:none;
    }
</style>
<script>
setTimeout(function(){
    [].forEach.call(document.querySelectorAll('.unhide15'), function (el) {
        el.style.display = 'block';
    });
}, 15000);

setTimeout(function(){
    [].forEach.call(document.querySelectorAll('.unhide30'), function (el) {
        el.style.display = 'block';
    });
}, 30000);

setTimeout(function(){
    [].forEach.call(document.querySelectorAll('.unhide60'), function (el) {
        el.style.display = 'block';
    });
}, 60000);
</script>

Any thoughts on how to do this?

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

0

I would use only one class... Along with a data attribute to hold the desired time.

document.querySelectorAll(".unhide").forEach(function (elem) {
  let delay = parseInt(elem.getAttribute("data-time")) * 1000;
  setTimeout(function () {
    elem.style.display = "block";
  }, delay);
});
.unhide{
  display: none;
}
<div class="unhide" data-time="1">Hello!</div>
<div class="unhide" data-time="3">Hello!</div>
<div class="unhide" data-time="10">Hello!</div>
<div class="unhide" data-time="12">Hello!</div>

=== EDIT

Since you cannot add data- attributes... You can have an array (on the JS side) to do the same thing about time values. Just make sure to have all them... And in order.

// Set a time array for each element (in order)
let time_array = [
  1, 3, 10, 12 // seconds
];

document.querySelectorAll(".unhide").forEach(function (elem, index) {
  let delay = time_array[index] * 1000;
  setTimeout(function () {
    elem.style.display = "block";
  }, delay);
});
.unhide{
  display: none;
}
<div class="unhide">Hello!</div>
<div class="unhide">Hello!</div>
<div class="unhide">Hello!</div>
<div class="unhide">Hello!</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