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

173
Visualizações
Dynamically creating images with a random lifespan

Every 0-2 seconds I'm creating an image at a random location within a div like this

setTimeout("addImage()", Math.floor((Math.random() * 2000) + 1));

I want these images to disappear 3-5 seconds after they're created. Using $('img').first().remove() I can remove the oldest image, but I want to specifically remove them at a time after they're created, not just the oldest one. This will suffice if there is no other way, but it isn't ideal.

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

0

Create them, then set a timeout for them individually.

setInterval("addImage()", Math.floor((Math.random() * 2000) + 1));

function addImage() {
  var tmpImg = new Image();
  tmpImg.src = 'https://picsum.photos/200?' + new Date().getTime();
  $('#imgs').prepend(tmpImg);
  tmpImg.onload = function() {
    setTimeout(() => {
      $(this).fadeOut()
    }, 4000);
  };
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='imgs'></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