简体   繁体   English

Javascript - 实现加载微调器的最佳方式

[英]Javascript - Best way to implement loading spinner

New to web-development and trying wrap my head around handling the DOM.网络开发的新手,并尝试全神贯注于处理 DOM。 Want to implement a spinner that appears while data is being processed and disappears after.想要实现一个在处理数据时出现并在之后消失的微调器。 I figure the order should be something like this我认为顺序应该是这样的

  1. on button press, make spinner visible and call data function按下按钮时,使微调器可见并调用数据 function
  2. after data function is finished make spinner invisible again数据 function 完成后再次使微调器不可见

Using Bootstrap 5使用引导程序 5

Current implementation is something along these lines:当前的实现是沿着这些方向的:

document.getElementById("button").addEventListener("click",function(){document.getElementById("spinner").classList.toggle("invisible");})
document.getElementById("button").addEventListener("click",dataProcess());
document.getElementById("button").addEventListener("click",function(){document.getElementById("spinner").classList.toggle("invisible");})

I understand the order in which event listeners are added are the order they are executed in but the spinner doesn't toggle at because the DOM doesn't update until all the functions are executed(since it toggled twice by then it's just invisible again).我知道添加事件侦听器的顺序是它们执行的顺序,但是微调器不会切换,因为 DOM 在执行所有函数之前不会更新(因为它切换了两次,然后它再次不可见) . Is there a way to update the DOM between events or a better way to implement the spinner altogether?有没有一种方法可以在事件之间更新 DOM,或者有更好的方法来完全实现微调器?

document.getElementById("button").addEventListener("click",()=>{
    setTimeOut(()=>{
        document.getElementById("spinner").classList.toggle("invisible")
    },600)
    dataProcess();
    document.getElementById("spinner").classList.toggle("invisible")
});

Give a callback function to dataprocess function and make button invisible给数据处理 function 回调 function 并使按钮不可见

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

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