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

195
Views
Refresh images and async, wait for load monitor errors

I want to refresh images in a page and in case the image gives me error loading i want to revert to the old cached image.

Can anyone help me?

Thanks.

var curtime = new Date().getTime();
var arrayimg = ["https://www.learningcontainer.com/wp-content/uploads/2020/07/Sample-JPEG-Image-File-Download.jpg?" + curtime, "https://upload.wikimedia.org/wikipedia/en/9/95/Test_image.jpg?" + curtime, "https://testimages.org/img/testimages_screenshot.jpg?" + curtime];
var arrayimgLoad = [1, 1, 1];
var arrayimgOLD = ["", "", ""];
var myVar = setInterval(function() {
  myTimer()
}, 1000);

function myTimer() {
  arrayimg.forEach(imageupdate);
}

function imageupdate(element, index, array) {
  imgname = "img" + index;
  if (arrayimgLoad[index] == 1) {
    arrayimgOLD[index] = document.getElementById(imgname).src;
    document.getElementById(imgname).src = element + new Date().getTime();
    loadImage(document.getElementById(imgname).src, index);
  }
}
async function loadImage(imageUrl, asindex) {
  arrayimgLoad[asindex] = 0;
  let img;
  const imageLoadPromise = new Promise(resolve => {
    img = new Image();
    img.onload = resolve;
    img.src = imageUrl;
  });
  await imageLoadPromise;
  arrayimgLoad[asindex] = 1;
  console.log("Image " + asindex + " Loaded");
  return img;
}
<img src="" id='img0' width="30%" height="30%" />
<img src="" id='img1' width="30%" height="30%" />
<img src="" id='img2' width="30%" height="30%" />

about 4 years ago · Juan Pablo Isaza
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!