Created
June 30, 2014 20:40
-
-
Save mohsentaleb/ec92bac57621d455a9e8 to your computer and use it in GitHub Desktop.
JavaScript Once Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The "once" wrapper | |
| function once(fn, context) { | |
| var executed = false; | |
| return function() { | |
| if(executed) return; | |
| executed = true; | |
| fn.apply(context || this, arguments); | |
| }; | |
| } | |
| var canOnlyFireOnce = once(function() { | |
| console.log('Fired!'); | |
| }); | |
| canOnlyFireOnce(); // "Fired!" | |
| canOnlyFireOnce(); // nada |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment