我的函式呼叫如下:
await insertingMatchIdsInAllTeamPlayers(fieldersA, matchID)
假設使用 matchID '1' 呼叫該函式,它應該被執行,但是如果使用 matchId '1' 再次呼叫該函式(在我的情況下會),則不應執行該函式。但是,如果它以 id '2'(基本上是 id !== '1')呼叫,它應該被執行。我不在乎外野手的爭論。
uj5u.com熱心網友回復:
您可以在函式外部的陣列中跟蹤所有傳遞的引數。當您呼叫該函式時,它會檢查所提供的引數是否在陣列中。如果不是,則呼叫該函式并將引數插入到陣列中。如果引數已經在陣列中,則不要呼叫該函式。
const suppliedMatchIDs = [];
function insertingMatchIdsInAllTeamPlayers(fieldersA, matchID) {
if (suppliedMatchIDs.includes(matchID)) {
return;
} else {
suppliedMatchIDs.push(matchID);
}
// Your function here
}
快取引數以加速函式呼叫的一般概念稱為memoization。
uj5u.com熱心網友回復:
const matchIdFirstTimeOne = true
if(matchId === 1 && matchIdFirstTimeOne) {
await insertingMatchIdsInAllTeamPlayers(fieldersA, matchID);
matchIFirstTimeOne = false
}
uj5u.com熱心網友回復:
使用閉包可以解決它。
var insertingMatchIdsInAllTeamPlayers = (function() {
var executed = [];
return function(fieldersA,val) {
if (executed.indexOf(val) == -1) {
executed.push(val);
console.log(val);
}
};
})();
insertingMatchIdsInAllTeamPlayers('',1); // console.log(1)
insertingMatchIdsInAllTeamPlayers('',1); //
insertingMatchIdsInAllTeamPlayers('',2); // console.log(2)
insertingMatchIdsInAllTeamPlayers('',2); //
insertingMatchIdsInAllTeamPlayers('',3); // console.log(3)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348987.html
標籤:javascript 节点.js 功能
下一篇:使用while回圈計算R函式
