Mi llamada de función se da a continuación:
await insertingMatchIdsInAllTeamPlayers(fieldersA, matchID)Supongamos que la función se llama con matchID '1', debería ejecutarse, pero si la función se llama de nuevo con matchId '1' (lo hará en mi caso), no debería ejecutarse. Sin embargo, si se llama con id '2' (básicamente id !== '1'), debería ejecutarse. No me importan los jardineros Un argumento.
Puede rastrear todos los argumentos pasados en una matriz fuera de la función. Cuando llame a la función, verificará si el argumento proporcionado está en la matriz. Si no es así, llame a la función e inserte el argumento en la matriz. Si el argumento ya está en la matriz, no llame a la función.
const suppliedMatchIDs = []; function insertingMatchIdsInAllTeamPlayers(fieldersA, matchID) { if (suppliedMatchIDs.includes(matchID)) { return; } else { suppliedMatchIDs.push(matchID); } // Your function here }El concepto general de almacenamiento en caché de argumentos para acelerar las llamadas a funciones se llama memoización .
const matchIdFirstTimeOne = true if(matchId === 1 && matchIdFirstTimeOne) { await insertingMatchIdsInAllTeamPlayers(fieldersA, matchID); matchIFirstTimeOne = false }Usar el cierre podría resolverlo.
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)