Skip to content

Instantly share code, notes, and snippets.

@rameshanandakrishnan
Created August 12, 2015 16:26
Show Gist options
  • Save rameshanandakrishnan/b47ac1c31fd8e765c598 to your computer and use it in GitHub Desktop.
Save rameshanandakrishnan/b47ac1c31fd8e765c598 to your computer and use it in GitHub Desktop.
Call 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