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

176
Visualizações
a function that adds a random image shows the same image everytime

I have a simple function that creates an image and the src is a random image from splash, but every time i call the function it generates the same image, why and how to fix it

function randomImage(){
    const img = document.createElement('img')
    img.src = 'https://source.unsplash.com/random/500x500'
    document.body.appendChild(img)
}

As far as i understand each function call is dependent from each other so why each function call in this example generates the same image despite the link itself should generate a random link

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

0

You do not generate a random link. You depend on the source.unsplash.com to return a random image.

But since your browser page has seen that url, it loads it from the cache.

You could add something random, to make the browser think it is a different url.

img.src = 'https://source.unsplash.com/random/500x500&timestamp=' + Date.now()
about 4 years ago · Juan Pablo Isaza Relatório

0

You could simply add a cache breaker to your URL, since the browser caches your request and returns the same image.

img.src = 'https://source.unsplash.com/random/500x500?t=' + new Date().getTime()

See also: Refresh image with a new one at the same url

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