`How to wait for an image to load?
K
K
kirillleogky2020-05-14 21:12:22
JavaScript
kirillleogky, 2020-05-14 21:12:22

How to wait for an image to load?

There is an asynchronous image acquisition:

let backgroundImg = await fetch(`https://api.unsplash.com/photos/random?orientation=landscape&per_page=1&query=${city},${currWeather},${timeOfTheYear},${timeOfDay}&client_id=${token}`);
    backgroundImg = await backgroundImg.json();
    backgroundImg = await backgroundImg.urls;
    backgroundImg = await backgroundImg.full;


Next, I want to know if the photo has loaded Tell me if
there is a better way than the one I did? :
const img = new Image();
  img.src = backgroundImg;
  img.onload = () => {
    console.log('YES');
  };

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question