mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-04 17:07:49 +00:00
Adding _.once ... Issue #121
This commit is contained in:
@@ -474,17 +474,17 @@
|
||||
_.debounce = function(func, wait) {
|
||||
return limit(func, wait, true);
|
||||
};
|
||||
|
||||
// Runs a function only if it's never been run before.
|
||||
|
||||
// Returns a function that will be executed at most one time, no matter how
|
||||
// often you call it. Useful for lazy initialization.
|
||||
_.once = function(func) {
|
||||
if (_.indexOf(onceRunFunctions, func)) {
|
||||
onceRunFunctions.push(func);
|
||||
return func.apply(func, slice.call(arguments, 1));
|
||||
}
|
||||
}
|
||||
|
||||
// Internal record of functions that have been run via `_.once`.
|
||||
var onceRunFunctions = [];
|
||||
var ran = false, memo;
|
||||
return function() {
|
||||
if (ran) return memo;
|
||||
ran = true;
|
||||
return memo = func.apply(this, arguments);
|
||||
};
|
||||
};
|
||||
|
||||
// Returns the first function passed as an argument to the second,
|
||||
// allowing you to adjust arguments, run code before and after, and
|
||||
|
||||
Reference in New Issue
Block a user