Skip to content

Instantly share code, notes, and snippets.

@natasv
Created January 21, 2016 19:08
Show Gist options
  • Save natasv/7d5b049ab1e21c47671a to your computer and use it in GitHub Desktop.
Save natasv/7d5b049ab1e21c47671a to your computer and use it in GitHub Desktop.
function once(fn, context) {
var result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
return result;
};
}
//example
var canOnlyFireOnce = once(function() {
console.log('yes!');
});
canOnlyFireOnce(); // "yes!"
canOnlyFireOnce(); // no
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment