首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >延迟字符串函数?

延迟字符串函数?
EN

Stack Overflow用户
提问于 2022-06-27 23:40:22
回答 1查看 40关注 0票数 0

基本上,我所有的代码都在一个while循环内。其中,我有一个for循环,它检查邻里网格中的蜜蜂。当它检测到蜜蜂时,它会调用一个函数。我需要调用它的所有函数等待并运行,只在while循环即将完成时(在for循环之外,但在while循环内)。

这是我的代码:

代码语言:javascript
复制
Cell.prototype.floodFillEach = function() {
  grid[orginalX][orginalY].floodFill();
  while(repeat<=7){
    for (var xoff = 0-x12; xoff <= 0+x12; xoff++) {
      for (var yoff = 0; yoff <= 0+x12; yoff++) {
        var i = orginalX + xoff;
        var j = orginalY + yoff;
        if (i > -1 && i < cols && j > 0 && j < rows) {
          var neighbour = grid[i][j];
          if (neighbour.marked2) {
            console.log("x:"+i+"  y:"+j);
            neighbour.floodFill(); // everytime this function is called within this while loop i need it to wait untill...       
          }
        }
      }
    }
      // ... here. I need the functions called to wait and run here and after they are done only then proceed with the rest of this while loop
    x12++
    repeat++
  }
}
EN

回答 1

Stack Overflow用户

发布于 2022-06-27 23:43:55

存储在数组中,然后在末尾调用。

代码语言:javascript
复制
Cell.prototype.floodFillEach = function() {
  grid[orginalX][orginalY].floodFill();
  while(repeat<=7){
    const neighboursToFloodFill = [];
    for (var xoff = 0-x12; xoff <= 0+x12; xoff++) {
      for (var yoff = 0; yoff <= 0+x12; yoff++) {
        var i = orginalX + xoff;
        var j = orginalY + yoff;
        if (i > -1 && i < cols && j > 0 && j < rows) {
          var neighbour = grid[i][j];
          if (neighbour.marked2) {
            console.log("x:"+i+"  y:"+j);
            neighboursToFloodFill.push(neighbour);     
          }
        }
      }
    }
    for (const neighbour of neighboursToFloodFill) {
      neighbour.floodFill();
    }
    x12++
    repeat++
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72779318

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档