Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

185
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda