我有以下两个功能:
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);
}
}
由于
答案 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);