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

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

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 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