Skip to content

Instantly share code, notes, and snippets.

@krabello
Created October 12, 2015 13:44
Show Gist options
  • Save krabello/69bc008a8207981749d5 to your computer and use it in GitHub Desktop.
Save krabello/69bc008a8207981749d5 to your computer and use it in GitHub Desktop.
Once for times when you prefer a given functionality only happen once, similar to the way you'd use an onload event.
function once(fn, context) {
var result;
return function() {
if(fn) {
result = fn.apply(context || this, arguments);
fn = null;
}
return result;
};
}
// Usage
var canOnlyFireOnce = once(function() {
console.log('Fired!');
});
canOnlyFireOnce(); // "Fired!"
canOnlyFireOnce(); // nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment