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

179
Visualizações
How to run a setTimeout after my page loads?

I want to run a time out function to show an image once the page is loaded but I don't know why it's not running. Is this a matter of syntax? Here's my code:

const screenshot = document.querySelector('#device-screenshot'); 
function loaded() {
console.log('loaded')
setTimeout(() => {screenshot.src = "dist/images/new-screenshot-2.jpeg"
  }, 4000); 
}

if (screenshot.complete) {
loaded()
} else {
screenshot.addEventListener('load', loaded)
screenshot.addEventListener('error', function() {
console.log('error')
 })
}













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

0

As I recall correctly, you don't need the curly braces in the setTimeout() function. Normal syntax could be as follows:

function loaded() {
    console.log('loaded')
    setTimeout(() => screenshot.src = "dist/images/new-screenshot-2.jpeg", 4000); 
}

If this doesn't work, then check to see if there is another way than .complete to check if it is loaded. I have never seen nor used it, but this looks like correct usage and the loaded() function should fire when the page loads. Alternatively, you can hook up an onload attribute to the body tag in the HTML file and set the value to 'loaded' or another function instead of waiting for the image to load.

about 4 years ago · Juan Pablo Isaza Relatório

0

What you are looking for is the onload event, you would use it like so:

document.body.addEventListener("load", loaded);
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