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

344
Vistas
Element doesn't add a class until certain code has executed, but I want it to do it before

I made a simple program in js that changes the aspect ratio of user uploaded images,I know that this is probably not the best way to do this sort of thing, but I think of it as practice.Here's the repo, and here's a live version.

There's a div with class .loading-screen that is supposed to cover the entire screen while the images are being processed, that div is initially hidden with a simple class that has a property of display : none;.

Once the user presses the "process" button for resizing the images, I try to remove the .hidden class from .loading-screen, but it doesn't remove it, it goes straight into processing the images, and once it's done it actually removes the class, but once the images have been finished processing, I add the class back again, so it's added and then removed immediately after. The basic flow relevant parts of the program is as follows :

const input = document.querySelector(".file-input");
const processButton = document.querySelector(".process");
let imageDivs = [];

input.addEventListener("change", () => {
  //load the images into the program, and store 
  //the resulting divs with images inside of the
  //imageDivs array
});

processButton.addEventListener("click", () => {
  const loadingScreen = document.querySelector(".loading-screen"); //this is the div that I want to cover the screen with

  loadingScreen.classList.remove("hidden"); //remove the '.hidden' class, so that it will cover the entire window

  let promises = [];
  
  for (let div of imageDivs) {
    let img = div.querySelector("img");

    //fit_image() draws the image in a canvas, and then 
    //returns a promise that resolves to the base64 data
    //of the canvas
    promises.push(fit_image(img));
  }

  Promise.all(promises).then(values => {
    loadingScreen.classList.add("hidden"); //hide this div once we are done processing the images

    //load the resized images into the program, etc...
  });
});

The problem is that loadingScreen.classList.remove("hidden") seems to run after the images have finished being processed, because if I remove the loadingScreen.classList.add("hidden") inside of the Promises.all() statement, the loadingScreen div appears after the images are done. How do I make it load the class before the images start processing?

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