lodash: Make deb ounce match throttle's return value behavior. [jddalton]

Former-commit-id: 2d4073c8a2ba20b98344c19cbfaf388a2683ef19
This commit is contained in:
John-David Dalton
2012-05-01 21:13:02 -04:00
parent 367f0bd6a9
commit d5e2489cad
2 changed files with 27 additions and 10 deletions

View File

@@ -1558,16 +1558,16 @@
}
/**
* Creates a new function that will delay its execution until after `wait`
* milliseconds have elapsed since the last time it was invoked. Pass `true`
* for `immediate` to cause debounce to invoke the function on the leading,
* Creates a new function that will delay the execution of `func` until after
* `wait` milliseconds have elapsed since the last time it was invoked. Pass
* `true` for `immediate` to cause debounce to invoke `func` on the leading,
* instead of the trailing, edge of the `wait` timeout.
*
* @static
* @memberOf _
* @category Functions
* @param {Function} func The function to debounce.
* @param {Number} wait The number of milliseconds to postone.
* @param {Number} wait The number of milliseconds to delay.
* @param {Boolean} immediate A flag to indicate execution is on the leading
* edge of the timeout.
* @returns {Function} Returns the new debounced function.
@@ -1577,26 +1577,30 @@
* jQuery(window).on('resize', lazyLayout);
*/
function debounce(func, wait, immediate) {
var args, thisArg, timeout;
var args,
result,
thisArg,
timeoutId;
function delayed() {
timeout = undefined;
timeoutId = undefined;
if (!immediate) {
func.apply(thisArg, args);
}
}
return function() {
var isImmediate = immediate && !timeout;
var isImmediate = immediate && !timeoutId;
args = arguments;
thisArg = this;
clearTimeout(timeout);
timeout = setTimeout(delayed, wait);
clearTimeout(timeoutId);
timeoutId = setTimeout(delayed, wait);
if (isImmediate) {
func.apply(thisArg, args);
result = func.apply(thisArg, args);
}
return result;
};
}

View File

@@ -99,6 +99,19 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.debounce');
(function() {
test('subsequent "immediate" debounced calls should return the result of the first call', function() {
var debounced = _.debounce(function(value) { return value; }, 100, true),
result = [debounced('x'), debounced('y')];
deepEqual(result, ['x', 'x']);
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.forEach');
(function() {