javascript - 将 for 循环的一部分放入 javascript 的函数中

标签 javascript for-loop

我有几个函数使用下面给出的 for 循环。

function startClaw(dir){
    var readCount = 0;
    for(var isRead in qdata){
        readCount++;
        if(qdata[isRead]['reading'] == true){
            return;
        }else if(readCount == 5){
            isAnimating = $("#claw").is(':animated');
            if(!isAnimating){// prevents multiple clicks during animation
                if(isMoving || isDropping){ return; }
                MCI = setInterval(function(){ moveClaw(dir); },10);
                //console.log("startClaw:" + dir);
                stopSwingClaw();    
            }
        }
    }
}
//.................................................................
function dropClaw(){
    var readCount = 0;
    for(var isRead in qdata){
        readCount++;
        if(qdata[isRead]['reading'] == true){
            return;
        }else if(readCount == 5){
            if(isDropping){ return; } //prevent multiple clicks
            stopSwingClaw();
            isDropping = true;  
            MCI = setInterval(moveDown,20); //start heartbeat
        }
    }
}

不同函数中 else if 语句中的所有内容都不同。我想知道是否有任何方法可以将 for 循环的“部分”放在 else if 外部,放入其自己的函数中。我觉得我很久以前就见过这个或做过这个,但我忘记了,我找不到任何例子。谢谢大家!

最佳答案

预览一下,我发现这和上面的类似。两个区别(看起来)是,计数被传递给函数,以防它们需要在 if 语句中进行不同的检查,并且它正在检查返回值是什么,因为它看起来像是从循环中返回 if条件满足。下面代码的注释中有注释。

function startClaw(dir) {
  // Pass a function as a callback to the method which expects to receive the count as a param
  doReadCount(qdata, function(theCount) { 
    if (theCount === 5) {
      isAnimating = $("#claw").is(':animated');
      if (!isAnimating) { // prevents multiple clicks during animation
        if (isMoving || isDropping) {
          return true;
        }
        MCI = setInterval(function() { moveClaw(dir); }, 10);
        //console.log("startClaw:" + dir);
        stopSwingClaw();
      }

      return false;
  });
}
//.................................................................
function dropClaw() {
  // Pass a function as a callback to the method which expects to receive the count as a param
  doReadCount(qdata, function(theCount) {
    if (theCount === 5) {
      if (isDropping) {
        return;
      } //prevent multiple clicks

      stopSwingClaw();
      isDropping = true;
      MCI = setInterval(moveDown,20); //start heartbeat
    }
  });
}

function doReadCount(qdata, elseFunction) {
  var readCount = 0;
  var elseReturn;
  for (var isRead in qdata) {
      readCount++;
      if (qdata[isRead]['reading'] == true) {
                return;
      } else {
        // call the function that was sent and pass it the current read count. If the return is true, then also return true here
        elseReturn = elseFunction(readCount);
        if (elseReturn) {
            return;
        }
      }
    }
  }

关于javascript - 将 for 循环的一部分放入 javascript 的函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43577789/

相关文章:

javascript - 使用 ajax 时,某些问题页面有一个查询字符串

Perl && 做 { 最后; };

python - 使用for循环在python中分配数组值

python - 如何编写 DRY Python For 循环

javascript - 最大化 ng-app 中的 div

javascript - 我无法在 React 组件中正确访问 redux 存储

javascript - 在 Angular 中,如何有效地将输入项拆分为数组

java - 如何使用 printf -Java 将二维数组的所有元素排列成完美的表格

javascript - For循环不显示我的范围

javascript - 为什么 waitFor() 忽略我的返回值?