mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Ensure _.debounce performs a trailing call, when both leading and trailing are true, only after the throttled function is called more than once. [closes #243]
Former-commit-id: 59ac6bc4ece373623f2bd753b662b8cc974cabc8
This commit is contained in:
14
lodash.js
14
lodash.js
@@ -4478,13 +4478,14 @@
|
||||
*/
|
||||
function debounce(func, wait, options) {
|
||||
var args,
|
||||
inited,
|
||||
result,
|
||||
thisArg,
|
||||
timeoutId,
|
||||
trailing = true;
|
||||
|
||||
function delayed() {
|
||||
timeoutId = null;
|
||||
inited = timeoutId = null;
|
||||
if (trailing) {
|
||||
result = func.apply(thisArg, args);
|
||||
}
|
||||
@@ -4497,15 +4498,15 @@
|
||||
trailing = 'trailing' in options ? options.trailing : trailing;
|
||||
}
|
||||
return function() {
|
||||
var isLeading = leading && !timeoutId;
|
||||
args = arguments;
|
||||
thisArg = this;
|
||||
|
||||
clearTimeout(timeoutId);
|
||||
timeoutId = setTimeout(delayed, wait);
|
||||
|
||||
if (isLeading) {
|
||||
if (!inited && leading) {
|
||||
inited = true;
|
||||
result = func.apply(thisArg, args);
|
||||
} else {
|
||||
timeoutId = setTimeout(delayed, wait);
|
||||
}
|
||||
return result;
|
||||
};
|
||||
@@ -4706,10 +4707,9 @@
|
||||
trailing = true;
|
||||
|
||||
function trailingCall() {
|
||||
lastCalled = new Date;
|
||||
timeoutId = null;
|
||||
|
||||
if (trailing) {
|
||||
lastCalled = new Date;
|
||||
result = func.apply(thisArg, args);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user