简体   繁体   English

在for循环的每次迭代之后添加延迟

[英]Add delay after each iteration of a for loop

First I want to say that I've read several answers regarding this issues like: 首先,我想说的是,我已经阅读了一些有关此问题的答案,例如:

JavaScript : For loop with timeout JavaScript:超时循环

Delay each iteration of loop by a certain time 将循环的每次迭代延迟一定时间

and some others but I just can't figure it out how exactly this works. 和其他一些,但我只是想不通这是如何工作的。

So after some experiments I end up with those two functions: 因此,经过一些实验,我最终得到了这两个功能:

function searchForPathWithDelay() {
    var isPathFound = false;
    for (var i = 0; i < cells.length; i = i + 1) {
        if (isThisTheGoalNode(cells[i].axisX, cells[i].axisY)) {
            //the below should be in own method
            var selectedNode = document.querySelector('[data-cellNumber="' + cells[i].id + '"]');
            selectedNode.style.backgroundColor = 'red';
            break;
        }
        setTimeout(searchForPath(cells[i].axisX, cells[i].axisY, cells[i].id), 500);
    }
}

and then: 接着:

function searchForPath(axisX, axisY, id) {
    if (isCellInBoundries(axisX, axisY)) {
        var selectedNode = document.querySelector('[data-cellNumber="' + id + '"]');
        selectedNode.style.backgroundColor = 'green';
    }
}

Since it's just a fun thing that I'm doing but the code is a lot I'm posting what I think is relevant to this question. 由于这只是我做的一件有趣的事,但是代码很多,因此我发布了我认为与该问题相关的内容。 There is no particular reason to have two methods since all the logic can be implemented in just one. 并不需要两种方法,因为所有逻辑都可以在一种方法中实现。 The separation was part of my attempts to add the delay I want. 分离是我尝试添加所需延迟的一部分。

Currently the logic itself is plain simple - cells is an array of objects, and each object provides a way to select a unique DOM elemten by using this: 当前,逻辑本身很简单- cells是一个对象数组,每个对象都提供了一种选择使用此元素的唯一DOM的方法:

document.querySelector('[data-cellNumber="' + id + '"]');

I'm not sure that it's becoming clear from the code I've pasted but the simple idea is that every time I select a cell and ultimately change it's background color I want to add some sort of delay so you can see the sequence in which the cells are painted. 我不确定从粘贴的代码中是否可以清楚看出来,但是一个简单的想法是,每次选择一个单元格并最终更改它的背景色时,我都想添加某种延迟,以便可以看到其中的顺序单元是粉刷的。

If you simply want to add delay for each iteration of loop, just change this line 如果您只是想为循环的每次迭代增加延迟,只需更改此行

setTimeout(searchForPath(cells[i].axisX, cells[i].axisY, cells[i].id), 500);

to

setTimeout(searchForPath(cells[i].axisX, cells[i].axisY, cells[i].id), 500 * (i+1));

This will ensure that setTimeout is invoke with incremental delays in each iteration. 这将确保setTimeout在每次迭代中以增量延迟被调用。

May be write a function like this? 可以这样写一个函数吗?

function loopWithDelay(array, callback, delay, counter){
   if(counter === undefined){
       counter = 0;
   }
   var element = array[counter];
   callback(element, counter);
   counter = counter + 1;
   if(counter < array.length){
      setTimeout(function(){
         loopWithDelay(array, callback, delay, counter);
      }, delay);
   }
}

And then call it like this: 然后这样称呼它:

loopWithDelay(cells, cb, 500);

where cb is your code that takes in the array element and index as arguments; 其中cb是将数组元素和索引作为参数的代码;

I have not tested it, but you get the idea. 我没有测试过,但是您知道了。

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

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