waynedunkley
6/4/2015 - 3:18 AM

Once - There are times when you prefer a given functionality only happen once, similar to the way you'd use an onload event. This code prov

Once - There are times when you prefer a given functionality only happen once, similar to the way you'd use an onload event. This code provides you said functionality

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