This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
precargarImagenes() { | |
// Mostrar la alerta | |
Swal.fire({ | |
title: "Cargando", | |
html: `Cargando imágenes...`, | |
allowOutsideClick: false, | |
allowEscapeKey: false, | |
}) | |
.then(this.reiniciarJuego) | |
// Ponerla en modo carga | |
Swal.showLoading(); | |
let total = this.imagenes.length, | |
contador = 0; | |
let imagenesPrecarga = Array.from(this.imagenes); | |
// También vamos a precargar la "espalda" de la tarjeta | |
imagenesPrecarga.push(NOMBRE_IMAGEN_OCULTA); | |
// Cargamos cada imagen y en el evento load aumentamos el contador | |
imagenesPrecarga.forEach(ruta => { | |
const imagen = document.createElement("img"); | |
imagen.src = ruta; | |
imagen.addEventListener("load", () => { | |
contador++; | |
if (contador >= total) { | |
// Si el contador >= total entonces se ha terminado la carga de todas | |
this.reiniciarJuego(); | |
Swal.close(); | |
} | |
}); | |
// Agregamos la imagen y la removemos instantáneamente, así no se muestra | |
// pero sí se carga | |
document.body.appendChild(imagen); | |
document.body.removeChild(imagen); | |
}); | |
}, | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment