Created
October 5, 2012 16:30
-
-
Save davetayls/3840850 to your computer and use it in GitHub Desktop.
Once function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Once script to make sure a function is only run once! | |
*/ | |
(function(global){ | |
function once(func) { | |
var ran = false, memo; | |
return function() { | |
if (ran) return memo; | |
ran = true; | |
memo = func.apply(this, arguments); | |
return memo; | |
}; | |
} | |
if (typeof exports !== 'undefined') { | |
module.exports = once; | |
} else if (global.define && global.define.amd) { | |
define(function(){ return once; }); | |
} else { | |
global.once = once; | |
} | |
}(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment