DOM 循环内的操作发生得太快

DOM manipulation inside loop happens too fast

尝试以不同颜色闪烁 div,并在它们之间间隔一段时间(不使用 jquery)。该程序在调试器中运行完美,但是当 运行 运行它时,所有更改发生得太快,用户什么也看不到。

尝试使用 setTimeout 无济于事(可能没有正确使用)

function makeBoard() {
var squareNum = 4
var selected
container = document.createElement('div')
container.id = 'container'
document.body.appendChild(container);
for (let index = 0; index < squareNum; index++) {
    squareDiv = document.createElement('div')
    squareDiv.className = 'square'
    squareDiv.id = '' + (index + 1)
    container.appendChild(squareDiv)
}
selected = document.getElementById('1')
selected.classList.add('selected')
return selected
}

function dimSwitch() {
var turnCnt = 1
var posIndex = 0
var selectedDivs = []
var tempCnt = 0
var tempIndex = 0
var timeNum = getMaxPos()
while (tempCnt < timeNum) {
    var posIndex = posArr.indexOf(turnCnt, tempIndex)
    tempIndex = posIndex + 1
    while (posIndex !== -1) {
        selectedDivs.push(document.getElementById(posIndex + 1 + ''))
        posIndex = posArr.indexOf(turnCnt, tempIndex)
        tempIndex = posIndex + 1
    } 
    selectDiv(selectedDivs) //After this i would like a small delay
    turnCnt++
    tempCnt++
    for (let index = 0; index < selectedDivs.length; index++) {
        selectedDivs[index].classList.remove('selected')
    }
    selectedDivs = []
}
}

function drawMove(currDiv, direction) {
 var nextDiv
 currDiv.classList.remove('selected')
 nextDiv = document.getElementById((parseInt(currDiv.id) + direction))
 nextDiv.classList.add('selected')
 return nextDiv
}

function selectDiv(divs) {
for (let index = 0; index < divs.length; index++) {
    divs[index].classList.add('selected')
}
}
function getMaxPos() {
var maxNum = 0
for (let index = 0; index < posArr.length; index++) {
    if (posArr[index] > maxNum) maxNum = posArr[index]
}
return maxNum
}

var TurnNum = 4 //Number of turns
var posArr = [1]
var turnCnt = 1
var currDiv = makeBoard()

document.onkeydown = function (event) {
switch (event.keyCode) {
    case 37:
        //Left Key -1
        posArr[turnCnt] = posArr[turnCnt - 1] - 1
        currDiv = drawMove(currDiv, -1)
        turnCnt++
        break;

    case 39:
        //Right key +1
        posArr[turnCnt] = posArr[turnCnt - 1] + 1
        currDiv = drawMove(currDiv, 1)
        turnCnt++
        break;

    case 40:
        currDiv.classList.remove('selected')
        dimSwitch()
        break;
}
if (turnCnt === TurnNum) {
    currDiv.classList.remove('selected')
    dimSwitch()
}
};

函数 selectDivs 应该 运行 每次执行之间有一些时间 每当使用延迟或超时时,它就会冻结或剂量正常工作 在我删除 for 循环中的 class 之前,用户应该能够看到哪些 div 是红色的 ('selected' class)。

JS FIDDLE FULL CODE

这就是我尝试使用 setTimeout 的方式,但其余代码将 运行ning 保留在后台,我看到的是所有红色的 div:

setTimeout(function(){ 
for (let index = 0; index < selectedDivs.length; index++) { 
       selectedDivs[index].classList.remove('selected') 
   } 
 },1000) 

您已将循环代码放在 setTimeout 块中,因此整个循环一目了然 运行,但在 1000 毫秒之后。如果你想让元素一个接一个出现1秒延迟,你可以把逻辑改成这样,为每个元素设置不同的超时时间(1000 * index)

for (let index = 0; index < selectedDivs.length; index++) { 
   setTimeout(function(){ 
        selectedDivs[index].classList.remove('selected') 
    }
    , (1000 * index)
    )
} 

简单的方法是进行递归循环,使用超时来延迟对自身的调用。下面的概念证明:

var childDivs = document.querySelectorAll('.child'); 

function selectDivs(divs, index, delay) {
  divs[index].classList.add('selected');
  
  delay = delay || 1000;  // if you want to change the delay
  index++
  
  if (index < divs.length) {
    setTimeout(function() {
      selectDivs(divs, index, delay);    
    }, delay);    
  }
}
.flex-container {
  display: flex;
  flex-wrap: wrap;
}

.child {
  height: 100px;
  width: 100px;
  box-sizing: border-box;

  background-color: yellow;
  border: 1px solid #000;
}

.selected {
  background-color: red;
}
<body onload="selectDivs(childDivs, 0)">

  <div class="flex-container">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
  </div>
  
</body>

我个人会尽可能使用 CSS,设置一个 CSS 变量来延迟更改。只是为了能够更容易地了解 DOM.

中发生的事情

var childDivs = document.querySelectorAll('.child'); 

function selectDivs(children) {
  let child = {};

  for (let i = 0; i < children.length; i++) {
    child = children[i];
    
    child.style.setProperty('--background-animation-delay', i+'s');
    child.classList.add('selected');  
  }
}
.flex-container {
  display: flex;
  flex-wrap: wrap;
}

.child {
  --background-animation-delay: 0s;
  
  height: 100px;
  width: 100px;
  box-sizing: border-box;

  background-color: #ffff00;
  border: 1px solid #000;
  
  transition: background-color 0s var(--background-animation-delay);
}

.selected {
  background-color: #ff0000;
}
<body onload="selectDivs(childDivs)">

  <div class="flex-container">
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
    <div class="child"></div>
  </div>
  
</body>