Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created June 12, 2015 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasvirkus/971ef388c04fc3a4c372 to your computer and use it in GitHub Desktop.
Save andreasvirkus/971ef388c04fc3a4c372 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;
};
}
// Usage
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