JavaScript - run once without booleans
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is there a way to run a piece of JavaScript code only ONCE , without using boolean flag variables to remember whether it has already been ran or not? Specifically not something like: var alreadyRan = false; function runOnce() { if (alreadyRan) { return; } alreadyRan = true; /* do stuff here */ } I'm going to have a lot of these types of functions and keeping all booleans would be messy... 回答1: An alternative way that overwrites a function when executed so it will be executed only once. function useThisFunctionOnce(){ // overwrite this