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

112
Views
How can make the loader display while fetching data?

Previous advice given to me was to start the loader before we fetch the data in the addNewDoggo function and adding an event listener to stop loader once data is processed. No loader appears while fetching data, can someone tell me why?

HTML

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="./doggos.css">
  <title>Dogs</title>
</head>

<body>
  <h1>Doggos</h1>
  <button class="add-doggo">Add Doggo</button>
  <div class="doggos">
    <div class="loader"><img src="./giphy (1).gif"></img></div>
  </div>

  <script src="./doggos.js"></script>
</body>

</html>

JS

const DOG_URL = "https://dog.ceo/api/breeds/image/random";

const doggos = document.querySelector(".doggos");


function addNewDoggo() {
    let loader = document.querySelector(".loader");
    const promise = fetch(DOG_URL);
    promise
        
        .then(function(response) {
            const processingPromise = response.json(); //This line of code parses the API response into a usuable js object
            return processingPromise; //this code returns a new promise and this process is called | PROCESS-CHAINING
        })

        .then(function(processedResponse) {
            const img = document.createElement("img");
            img.src = processedResponse.message;
            img.alt = "Cute doggo";
            doggos.appendChild(img);
        });
}

document.querySelector(".add-doggo") .addEventListener("click", addNewDoggo)

document.addEventListener('load', function() {
    loader.style.display = 'none';
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should read the docs on what the load event signifies — it's referring to page resources loading and not a promise that you've kicked off. Log inside the body of your event listener to show when it's finished.

You will need a different solution for what you're trying to achieve. There are many similar questions.

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!