2017-04-23 53 views
0

我有幾個函數使用下面給出的for循環。將for循環的部分放入javascript函數中

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聲明中的所有內容在各種函數中都不相同。我想知道是否有任何方法將else if外部的for循環的「部分」放入它自己的函數中。我覺得我已經看到了這一點,或者很早以前就已經這樣做了,但它逃脫了我,我找不到任何例子。感謝大家!

+0

當然,但這取決於你在這些不同的功能中實際需要做什麼。 –

+0

剛剛意識到我可能會抓住使用'for-in'循環的機會。它有目的地迭代數組對象,而不是按順序運行。在我的代碼中使用它的情況下很好。 – Jester

+0

這裏沒有足夠的知識 – charlietfl

回答

1

預覽,我看到這和上面類似。兩種不同(看起來像)是在這裏計數傳遞給函數,以防他們需要在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; 
     } 
     } 
    } 
    } 
+0

啊是的!回調函數正是我所期待的。這保持乾淨,仍然可以完成我需要的工作。萬分感謝!我真的沒有在幾年內做出回調函數(更不用說使用JavaScript了),所以這就是爲什麼我忘了。 – Jester

1

您可以將函數傳遞到另一個函數來實現此目的。我已經完成了dropClaw,從我的例子中應該清楚如何提取startClaw

function operateClaw(func){ 
    var readCount = 0; 
    for(var isRead in qdata){ 
     readCount++; 
     if(qdata[isRead]['reading'] == true){ 
      return; 
     }else if(readCount == 5){ 
      func(); 
     } 
    } 
} 

function drop() { 
    if(isDropping){ return; } //prevent multiple clicks 
    stopSwingClaw(); 
    isDropping = true; 
    MCI = setInterval(moveDown,20); //start heartbeat 
} 

function dropClaw() { 
    operateClaw(drop); 
}