JavaScript: once

Published on Author craigzearfossLeave a comment
/*
Execute a function only once.

reference: http://davidwalsh.name/essential-javascript-functions
*/
function once(fn, context) {
   var result;

   return function() {
       if(fn) {
           result = fn.apply(context || this, arguments);
           fn = null;
       }

       return result;
   };
}

Leave a Reply

Your email address will not be published. Required fields are marked *