mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-08 18:17:48 +00:00
lodash: Make deb ounce match throttle's return value behavior. [jddalton]
Former-commit-id: 2d4073c8a2ba20b98344c19cbfaf388a2683ef19
This commit is contained in:
24
lodash.js
24
lodash.js
@@ -1558,16 +1558,16 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new function that will delay its execution until after `wait`
|
* Creates a new function that will delay the execution of `func` until after
|
||||||
* milliseconds have elapsed since the last time it was invoked. Pass `true`
|
* `wait` milliseconds have elapsed since the last time it was invoked. Pass
|
||||||
* for `immediate` to cause debounce to invoke the function on the leading,
|
* `true` for `immediate` to cause debounce to invoke `func` on the leading,
|
||||||
* instead of the trailing, edge of the `wait` timeout.
|
* instead of the trailing, edge of the `wait` timeout.
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Functions
|
* @category Functions
|
||||||
* @param {Function} func The function to debounce.
|
* @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
|
* @param {Boolean} immediate A flag to indicate execution is on the leading
|
||||||
* edge of the timeout.
|
* edge of the timeout.
|
||||||
* @returns {Function} Returns the new debounced function.
|
* @returns {Function} Returns the new debounced function.
|
||||||
@@ -1577,26 +1577,30 @@
|
|||||||
* jQuery(window).on('resize', lazyLayout);
|
* jQuery(window).on('resize', lazyLayout);
|
||||||
*/
|
*/
|
||||||
function debounce(func, wait, immediate) {
|
function debounce(func, wait, immediate) {
|
||||||
var args, thisArg, timeout;
|
var args,
|
||||||
|
result,
|
||||||
|
thisArg,
|
||||||
|
timeoutId;
|
||||||
|
|
||||||
function delayed() {
|
function delayed() {
|
||||||
timeout = undefined;
|
timeoutId = undefined;
|
||||||
if (!immediate) {
|
if (!immediate) {
|
||||||
func.apply(thisArg, args);
|
func.apply(thisArg, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return function() {
|
return function() {
|
||||||
var isImmediate = immediate && !timeout;
|
var isImmediate = immediate && !timeoutId;
|
||||||
args = arguments;
|
args = arguments;
|
||||||
thisArg = this;
|
thisArg = this;
|
||||||
|
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeoutId);
|
||||||
timeout = setTimeout(delayed, wait);
|
timeoutId = setTimeout(delayed, wait);
|
||||||
|
|
||||||
if (isImmediate) {
|
if (isImmediate) {
|
||||||
func.apply(thisArg, args);
|
result = func.apply(thisArg, args);
|
||||||
}
|
}
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
test/test.js
13
test/test.js
@@ -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');
|
QUnit.module('lodash.forEach');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user