Skip to content

Instantly share code, notes, and snippets.

@etoxin
Created November 3, 2014 05:56
Show Gist options
  • Save etoxin/2a6de98e8a8080d40f3f to your computer and use it in GitHub Desktop.
Save etoxin/2a6de98e8a8080d40f3f to your computer and use it in GitHub Desktop.
Run a function Once
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