在加载DOM元素(循环)上的异步无效

发布于 2025-02-11 17:44:13 字数 1916 浏览 0 评论 0原文

我正在尝试在JS中像探险家一样。我创建了一个类Explorer,该类Explorer在父母的DOM A DIV(Explorer)中加载,然后在Div1中的DOM A DIV2(曲目)中加载。然后,使用已知的曲目,我需要将DOM Divs加载到Div2(曲目)中,不同步以不阻止其余的。

我试图做到这一点,但它无法正如我所描述的那样起作用,DIV都会一次加载,因此它阻止了其他动作。我们应该看到红色div(Div1:Explorer),然后看到蓝色Div(Div2:曲目),然后一个一个一个一个一个divs。

有人可以帮我吗?

我做了这个Codpen来说明,这是一个SIPMLIFIED代码。 该按钮象征我在过程中要执行的其他操作。 (( 转到第54行,然后更改执行循环以了解我的问题的阵列。 )

https://codepen.io/dragoleforgeron/pen/pen/pen/pen/pen/ylklekleqm

class Explorer {
  
  parent;
  rep1;
  rep2;
  
  constructor(){
    this.parent = document.getElementById("parent");

    this.rep1 = [/*some object data*/];
    this.rep2 = [/*same as rep1 except that he's bigger*/];

    this.loadExplorer();
  }
  
  
  loadExplorer() {
    
    // Load explorer in parent
    let explorer = document.createElement("div");
    explorer.id = "explorer";
    explorer.classList.add("Explorer");
    this.parent.appendChild(explorer);
    
    // Load repertory container in explorer
    let repertory = document.createElement("div");
    repertory.id = "repertory";
    repertory.classList.add("Repertory");
    explorer.appendChild(repertory);
    
    this.loadRep().then(() => console.log("do some stuff after"));
  }
  
  
  async loadRep() {
    
    // Try to use rep2 instead of rep1, you will see that the explorer and repertory will be load after going through the rep (btw it's the same for rep1 but we feel it less)
    await this.rep1.map(async entry => {
      
      // Load div
      let div = document.createElement("div");
      div.id = entry.name;
      div.classList.add("Entry");
      div.innerText = entry.name;
      
      await document.getElementById("repertory").appendChild(div);
    });
  }
  
}

let explorer1 = new Explorer();

前进寻求帮助。

i'm trying to make an explorer-like in js. I created a class Explorer who load in the DOM a div (explorer) in a parent, then load in the DOM a div2 (repertory) in the div1. Then, with a knowed repertory, i need to load in DOM divs into div2 (repertory) asynchronously not to block the rest.

i trying to do it but it doesn't work as i described, the divs are all loaded at once so it block the other actions. We should see the red div (div1 : explorer) then the blue div (div2 : repertory) and then the divs loaded one by one.

can someone help me ?

I did this codpen to illustrate, it's a sipmlified code.
The Button symbolize the other actions i want to do during process.
(
Go to line 54 then change the array on which the loop is performed to understand my problem.
)

https://codepen.io/DragoLeForgeron/pen/yLKLeqm

class Explorer {
  
  parent;
  rep1;
  rep2;
  
  constructor(){
    this.parent = document.getElementById("parent");

    this.rep1 = [/*some object data*/];
    this.rep2 = [/*same as rep1 except that he's bigger*/];

    this.loadExplorer();
  }
  
  
  loadExplorer() {
    
    // Load explorer in parent
    let explorer = document.createElement("div");
    explorer.id = "explorer";
    explorer.classList.add("Explorer");
    this.parent.appendChild(explorer);
    
    // Load repertory container in explorer
    let repertory = document.createElement("div");
    repertory.id = "repertory";
    repertory.classList.add("Repertory");
    explorer.appendChild(repertory);
    
    this.loadRep().then(() => console.log("do some stuff after"));
  }
  
  
  async loadRep() {
    
    // Try to use rep2 instead of rep1, you will see that the explorer and repertory will be load after going through the rep (btw it's the same for rep1 but we feel it less)
    await this.rep1.map(async entry => {
      
      // Load div
      let div = document.createElement("div");
      div.id = entry.name;
      div.classList.add("Entry");
      div.innerText = entry.name;
      
      await document.getElementById("repertory").appendChild(div);
    });
  }
  
}

let explorer1 = new Explorer();

thanks by advance for help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文