简体   繁体   English

如何将 CSS 动画延迟与 NodeJS 延迟对齐?

[英]How to align CSS animation-delay in with NodeJS delay?

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:当我的 setTimeout 在 NodeJS 中时,如何添加预加载器,下面是调用 api 并将延迟设置为 10 秒的代码:

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:我试图像这样使用前端 js 添加加载程序:

window.addEventListener("load", function () {
    const loader = document.querySelector(".loader");
    loader.className += " hidden"; // class "loader hidden"
});

HTML: HTML:

<div class="loader">
    <img src="/public/images/coffee-loading.gif" alt="Loading..." />
</div>

CSS: 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.它的工作原理是,由于 setTimeout,它应该等待 10 秒,然后再显示预加载器 10 秒,而不是与延迟对齐。

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.我应该指出,这在用户体验方面非常糟糕,除非您计划减少这些计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM