Skip to content

Instantly share code, notes, and snippets.

@davetayls
Created October 5, 2012 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetayls/3840850 to your computer and use it in GitHub Desktop.
Save davetayls/3840850 to your computer and use it in GitHub Desktop.
Once function
/**
* 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