How do I add a preloader when my setTimeout is in NodeJS, below is the code to call the api and set a delay for 10s:
function delay(s){
return new Promise(resolve => setTimeout(resolve,s));
}
async function getXml(){
let ans;
await delay(10000)
const {data} = await axios.get('https://gist.githubusercontent.com/SwayamShah97/a3619c5828ac8ed8085c4ae295a855d9/raw/e4e372552e042bd8bd9e8ab87da93eb030114f86/people.xml');
xml2js.parseString(data, (err, result) => {
if(err) {
throw err;
}
ans = result
});
I have tried to add the loader using frontend js like this:
window.addEventListener("load", function () {
const loader = document.querySelector(".loader");
loader.className += " hidden"; // class "loader hidden"
});
HTML:
<div class="loader">
<img src="/public/images/coffee-loading.gif" alt="Loading..." />
</div>
CSS:
.loader.hidden {
animation: fadeOut 10s;
animation-fill-mode: forwards;
}
@keyframes fadeOut {
100% {
opacity: 0;
visibility: hidden;
}
}
How it works is, it waits 10s as it should due to the setTimeout, and then shows the preloader for additional 10s instead of aligning with the delay.
How can I achieve this functionality?
animation-delay: 10s;
I should note that this is pretty bad in terms of user experience unless you plan on reducing those timers.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.