Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

178
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!