English 中文(简体)
穿透大型阵容,而不在 Java
原标题:Looping through large array without freezing in JavaScript

我要说的是,我有10 000名随机分类的愤怒。 在通过该守则时,我想逐个增加<条码>。 如果我这样做的话,那是:

const myArray = [/* 10000 totally random integers! */];
let pos = 0;
while (pos < myArray.length) {
  document.getElementById("unorderedList").innerHTML += `<li>${pos}</li>`;
  pos++;
}
console.log("done!");

It freezes the whole window until it finishes, then it logs everything. How can I make it so the loop adds the number every iteration until it finishes instead of finishing the loop then printing everything? Note: this hypothetical function also needs to interact with a Window created with window.open, so I don t think I can use workers since that interacts with the DOM.

问题回答

您可使用<代码>requestAnimationFrame。

requestAnimationFrame is a JavaScript method used for smooth and efficient animations on the web.

It schedules a provided function to run before the next repaint, ensuring optimal performance by syncing with the browser s rendering cycle.

Unlike alternatives like setTimeout, it adapts to the screen refresh rate, resulting in visually smoother animations and better resource utilization.

它通常用于点im,用于更新阵地和重塑现场。

我添加以下样本代码。

const myArray = new Array(10000).fill(0).map((_, index) => index);
const batchSize = 30; //you can control speed by this variable
let pos = 0;
const unorderedList = document.getElementById("unorderedList");

const updateDOM = () => {
  const fragment = document.createDocumentFragment();

  while (pos < myArray.length && fragment.children.length < batchSize) {
    const li = document.createElement( li );
    li.textContent = myArray[pos++];
    fragment.appendChild(li);
  }

  unorderedList.appendChild(fragment);

  pos < myArray.length ? requestAnimationFrame(updateDOM) : stop();
};

const start = () => requestAnimationFrame(updateDOM);
const stop = () => console.log("done!");

start();
<div id="unorderedList"></div>

Issue

这是因为浏览器在整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整整

Solution

您可使用

const myArray = [/* 10000 totally random integers! */];
let pos = 0;

function processArrayChunk() {
  const ul = document.getElementById("unorderedList");
  const chunkSize = 50; // adjust it according to your myArray size

  for (let i = 0; i < chunkSize && pos < myArray.length; i++) {
    const li = document.createElement("li");
    li.textContent = myArray[pos];
    ul.appendChild(li);
    pos++;
  }

  if (pos < myArray.length) {
    requestAnimationFrame(processArrayChunk);
  } else {
    console.log("done!");
  }
}

processArrayChunk();




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签