lodash: Reduce functions created in debounce and throttle and make each work when called recursively. [int3, jddalton]

Former-commit-id: 8d8d1966f7b8710d1bd51c830c4d3c08643ba21a
This commit is contained in:
John-David Dalton
2012-05-01 00:05:26 -04:00
parent 0b404d4bb1
commit 6fb7681a2d

View File

@@ -1335,7 +1335,7 @@
: indexOf(seen, computed) < 0 : indexOf(seen, computed) < 0
) { ) {
seen.push(computed); seen.push(computed);
result.push(array[index]) result.push(array[index]);
} }
} }
return result; return result;
@@ -1528,7 +1528,7 @@
} }
/** /**
* Creates a new function that will postpone its execution until after `wait` * Creates a new function that will delay its execution until after `wait`
* milliseconds have elapsed since the last time it was invoked. Pass `true` * milliseconds have elapsed since the last time it was invoked. Pass `true`
* for `immediate` to cause debounce to invoke the function on the leading, * for `immediate` to cause debounce to invoke the function on the leading,
* instead of the trailing, edge of the `wait` timeout. * instead of the trailing, edge of the `wait` timeout.
@@ -1547,21 +1547,26 @@
* jQuery(window).on('resize', lazyLayout); * jQuery(window).on('resize', lazyLayout);
*/ */
function debounce(func, wait, immediate) { function debounce(func, wait, immediate) {
var timeout; var args, thisArg, timeout;
return function() {
var args = arguments,
thisArg = this;
if (immediate && !timeout) { function delayed() {
timeout = undefined;
if (!immediate) {
func.apply(thisArg, args); func.apply(thisArg, args);
} }
}
return function() {
var isImmediate = immediate && !timeout;
args = arguments;
thisArg = this;
clearTimeout(timeout); clearTimeout(timeout);
timeout = setTimeout(function() { timeout = setTimeout(delayed, wait);
timeout = undefined;
if (!immediate) { if (isImmediate) {
func.apply(thisArg, args); func.apply(thisArg, args);
} }
}, wait);
}; };
} }
@@ -1681,29 +1686,32 @@
* jQuery(window).on('scroll', throttled); * jQuery(window).on('scroll', throttled);
*/ */
function throttle(func, wait) { function throttle(func, wait) {
var args, more, result, thisArg, throttling, timeout, var args, more, thisArg, throttling, timeout,
whenDone = debounce(function() { more = throttling = false; }, wait); whenDone = debounce(function() { more = throttling = false; }, wait);
function delayed() {
timeout = undefined;
if (more) {
func.apply(thisArg, args);
}
whenDone();
}
return function() { return function() {
var result;
args = arguments; args = arguments;
thisArg = this; thisArg = this;
if (!timeout) { if (!timeout) {
timeout = setTimeout(function() { timeout = setTimeout(delayed, wait);
timeout = undefined;
if (more) {
func.apply(thisArg, args);
}
whenDone();
}, wait);
} }
if (throttling) { if (throttling) {
more = true; more = true;
} else { } else {
throttling = true;
result = func.apply(thisArg, args); result = func.apply(thisArg, args);
} }
whenDone(); whenDone();
throttling = true;
return result; return result;
}; };
} }