関数にifステートメントがあります。関数の最初の呼び出しでのみifステートメントを実行するにはどうすればよいですか?
あなたはこれを使うことができます
var notFirstRun = false;
function myFunction(){
if (notFirstRun) return; // this will "exit" the function
else notFirstRun = true;
if (foo == bar){ // your if statement
// somecode
}
// rest of the function
}
またはよりスコープの広いイベント:
var myFunction = (function(notFirstRun) {
return function() {
if (notFirstRun) return; // this will "exit" the function
else notFirstRun = true;
if (foo == bar) { // your if statement
// somecode
}
// rest of the function
}
})();
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加