How can I display the loader for 5 seconds at least or till when the assets have been rendered completely. I have a hero image that loads slowly so I hope the loader can delay the user seeing the page until the hero image has loaded half way.
This is my index.html file
<script src="https://unpkg.com/@lottiefiles/lottie-player@latest/dist/lottie-player.js"></script>
<div id="root"></div>
<div style="
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
">
<dotlottie-player id="myDotlottiePlayer" src="https://lottie.host/2e951bf0-ee8b-42ec-aa93-681a557e55c9/6zwzi5ZAks.json" background="transparent" speed="1" style="width: 300px; height: 300px" loop autoplay></dotlottie-player>
</div>
<script type="module">
// Add a timeout to hide the dotlottie-player after 5 seconds //
const dotlottiePlayer = document.getElementById("myDotlottiePlayer");
setTimeout(() => { dotlottiePlayer.style.display = "none"; }, 5000);
</script>
<script src="https://unpkg.com/@dotlottie/player-component@latest/dist/dotlottie-player.mjs" type="module"></script>
2
Answers
Create two Promises where one waits for a timeout that triggers after 5 seconds and where the other listens to the
load
event.Use
Promise.all()
to act on when all of the promises have resolved.You can just use event listener to check when the page is loaded then it could fire the timeout function to hide the loader and show up your content
here is an example using
addEventListener()