合并2个Javascript函数

时间:2014-05-28 13:26:55

标签: javascript function merge

我有以下两个功能:

function undo(){
    var card = discardPile.pop();
    if( card.col != -1 ){
        sendToCol( card );
    }else{
        sendToDrawPile( card );
    }
    cardsLeft();
}

function undowaste(){
    var card = wastePile.pop();
    if( card.col != -1 ){
        sendToCol( card );
    }else{
        sendToDrawPile( card );
    }
    cardsLeft();
}

除了card value之外,它们是相同的。所以我想知道,我可以合并这两个吗?怎么做?

修改 我使用这个函数执行两个:

function restart(){
    if( discardPile.length){
        undo();
        setTimeout(restart,75);
    }if( wastePile.length){
        undowaste();
        setTimeout(restart,75);
    }
}

由于

1 个答案:

答案 0 :(得分:4)

function undo(pile){
    var card = pile.pop();
    if( card.col != -1 ){
        sendToCol( card );
    }else{
        sendToDrawPile( card );
    }
    cardsLeft();
}

undo(wastePile);
undo(discardPile);

function undo(card){
    if( card.col != -1 ){
        sendToCol( card );
    }else{
        sendToDrawPile( card );
    }
    cardsLeft();
}

undo(wastePile.pop());
undo(discardPile.pop());

间隔运行

setInterval(function() {
  if(wastePile.length>0) undo(wastePile.pop());
  if(discardPile.length>0) undo(discardPile.pop());
},200);