Skip to content

Instantly share code, notes, and snippets.

@mohsentaleb
Created June 30, 2014 20:40
Show Gist options
  • Select an option

  • Save mohsentaleb/ec92bac57621d455a9e8 to your computer and use it in GitHub Desktop.

Select an option

Save mohsentaleb/ec92bac57621d455a9e8 to your computer and use it in GitHub Desktop.
JavaScript Once Function
// The "once" wrapper
function once(fn, context) {
var executed = false;
return function() {
if(executed) return;
executed = true;
fn.apply(context || this, arguments);
};
}
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