Skip to content

Instantly share code, notes, and snippets.

@rayou
Created June 14, 2015 05:29
Show Gist options
  • Save rayou/063305087db03976723e to your computer and use it in GitHub Desktop.
Save rayou/063305087db03976723e to your computer and use it in GitHub Desktop.
once.js
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
//http://davidwalsh.name/essential-javascript-functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment