javascript - 使用 javascript 在循环内设置超时

标签 javascript timeout sleep

我正在制作一个解谜函数,它使用当前打乱顺序的一系列拼图。每一 block 都有一个 id,它指向数组中的正确位置。我在要交换的部分上设置了叠加颜色。我希望在着色和交换碎片之间有一个延迟。

function solvePuzzle() {
    while (rezolvat == false) // while all pieces are not in correct position
        for (var i = 0; i < _piese.length; i++) { // iterates through array of puzzle pieces
            if (checkPiesa(i) == false) {
                _piesaCurentaDrop = _piese[i];
                _piesaCurenta = getPiesaDestinatie(_piesaCurentaDrop.id); // destination piece
                _context.save();
                _context.globalAlpha = .4;
                _context.fillStyle = PUZZLE_HOVER_TINT;
                _context.fillRect(_piesaCurentaDrop.xPos, _piesaCurentaDrop.yPos, _latimePiesa, _inaltimePiesa);
                _context.fillStyle = PUZZLE_DESTINATION_HOVER_TINT;
                _context.fillRect(_piesaCurenta.xPos, _piesaCurenta.yPos, _latimePiesa, _inaltimePiesa);
                _context.restore();

                // here I want to have some kind of 'sleep' for 2000 ms so you can see the pieces about to be swapped
                dropPiece(); // function for swapping puzzle pieces
            }
        }
}

最佳答案

您可以使用javascript的setTimeout()函数,该函数将在指定的毫秒后执行该函数,您可以了解更多信息 here

function solvePuzzle() {
  while (rezolvat == false) // while all pieces are not in correct position
    for (var i = 0; i < _piese.length; i++) { // iterates through array of puzzle pieces
      (function (i) {
        setTimeout(function () {

          if (checkPiesa(i) == false) {
            _piesaCurentaDrop = _piese[i];
            _piesaCurenta = getPiesaDestinatie(_piesaCurentaDrop.id); // destination piece
            _context.save();
            _context.globalAlpha = .4;
            _context.fillStyle = PUZZLE_HOVER_TINT;
            _context.fillRect(_piesaCurentaDrop.xPos, _piesaCurentaDrop.yPos, _latimePiesa, _inaltimePiesa);
            _context.fillStyle = PUZZLE_DESTINATION_HOVER_TINT;
            _context.fillRect(_piesaCurenta.xPos, _piesaCurenta.yPos, _latimePiesa, _inaltimePiesa);
            _context.restore();

            // here I want to have some kind of 'sleep' for 2000 ms so you can see the pieces about to be swapped
            // setTimeout in side task execution
            setTimeout(() => dropPiece(), 1000); // function for swapping puzzle pieces
          }
        }, 2000 * i); // schedules excution increasingly for each iteration
      })(i);
    }
}

在上面的代码中,for 循环立即完成,但是,它使用 setTimeout 在指定的增加时间 (i*2000) 后安排每次迭代的执行

因此执行,(尽管 for 循环立即完成)

第一次迭代将在 0*2000=0 毫秒时立即开始,

第二次执行的任务将在(1*2000)之后执行,

第三次执行的任务将在(2*2000)之后执行,

等等..

只是为了简单理解,请查看下面的示例代码

工作代码示例

for (var i = 0; i < 5; i++) {
  (function(i) {
    setTimeout(function() {
      console.log(i);
      setTimeout(() => console.log(i + 0.5), 1000); // setTimeout in side task execution in case needed
    }, 2000 * i); // schedules excution increasingly for each iteration
  })(i);
}

关于javascript - 使用 javascript 在循环内设置超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58700926/

相关文章:

用于检测移动浏览器屏幕宽度的 Javascript?

PHP:当循环很长的函数没有响应时退出并显示警报

递归函数的Javascript settimeout?

linux - nanosleep sleep 60 微秒太长

.net - 在.NET控制台应用程序中,防止Windows sleep /休眠

javascript - 向容器中插入内容

javascript - 根据 ng-repeat 的数据更改 Angular Directive(指令)元素的 CSS(或类)

javascript - 根据文本输入更改具有特定类的所有元素

sql - 如果服务器不存在,如何使 ADO 连接更快超时?

java - 在没有线程的情况下停止程序