mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 01:57:50 +00:00
Reduce the number of times clearTimeout is called in _.debounce.
Former-commit-id: 5b07b6660be4ff6783a8348fc8a122cfab5d10c4
This commit is contained in:
46
lodash.js
46
lodash.js
@@ -5375,6 +5375,7 @@
|
|||||||
function debounce(func, wait, options) {
|
function debounce(func, wait, options) {
|
||||||
var args,
|
var args,
|
||||||
result,
|
result,
|
||||||
|
stamp,
|
||||||
thisArg,
|
thisArg,
|
||||||
callCount = 0,
|
callCount = 0,
|
||||||
lastCalled = 0,
|
lastCalled = 0,
|
||||||
@@ -5395,24 +5396,29 @@
|
|||||||
maxWait = 'maxWait' in options && nativeMax(wait, options.maxWait || 0);
|
maxWait = 'maxWait' in options && nativeMax(wait, options.maxWait || 0);
|
||||||
trailing = 'trailing' in options ? options.trailing : trailing;
|
trailing = 'trailing' in options ? options.trailing : trailing;
|
||||||
}
|
}
|
||||||
var clear = function() {
|
|
||||||
clearTimeout(maxTimeoutId);
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
callCount = 0;
|
|
||||||
maxTimeoutId = timeoutId = null;
|
|
||||||
};
|
|
||||||
|
|
||||||
var delayed = function() {
|
var delayed = function() {
|
||||||
var isCalled = trailing && (!leading || callCount > 1);
|
var remaining = wait - (new Date - stamp);
|
||||||
clear();
|
if (remaining <= 0) {
|
||||||
if (isCalled) {
|
var isCalled = trailing && (!leading || callCount > 1);
|
||||||
lastCalled = +new Date;
|
callCount = 0;
|
||||||
result = func.apply(thisArg, args);
|
timeoutId = null;
|
||||||
|
if (maxTimeoutId) {
|
||||||
|
clearTimeout(maxTimeoutId);
|
||||||
|
maxTimeoutId = null;
|
||||||
|
}
|
||||||
|
if (isCalled) {
|
||||||
|
lastCalled = +new Date;
|
||||||
|
result = func.apply(thisArg, args);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
timeoutId = setTimeout(delayed, remaining);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var maxDelayed = function() {
|
var maxDelayed = function() {
|
||||||
clear();
|
callCount = 0;
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
maxTimeoutId = timeoutId = null;
|
||||||
if (trailing || (maxWait !== wait)) {
|
if (trailing || (maxWait !== wait)) {
|
||||||
lastCalled = +new Date;
|
lastCalled = +new Date;
|
||||||
result = func.apply(thisArg, args);
|
result = func.apply(thisArg, args);
|
||||||
@@ -5421,26 +5427,24 @@
|
|||||||
|
|
||||||
return function() {
|
return function() {
|
||||||
args = arguments;
|
args = arguments;
|
||||||
|
stamp = +new Date;
|
||||||
thisArg = this;
|
thisArg = this;
|
||||||
callCount++;
|
callCount++;
|
||||||
|
|
||||||
// avoid issues with Titanium and `undefined` timeout ids
|
|
||||||
// https://github.com/appcelerator/titanium_mobile/blob/3_1_0_GA/android/titanium/src/java/ti/modules/titanium/TitaniumModule.java#L185-L192
|
|
||||||
clearTimeout(timeoutId);
|
|
||||||
|
|
||||||
if (maxWait === false) {
|
if (maxWait === false) {
|
||||||
if (leading && callCount < 2) {
|
if (leading && callCount < 2) {
|
||||||
result = func.apply(thisArg, args);
|
result = func.apply(thisArg, args);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
var stamp = +new Date;
|
|
||||||
if (!maxTimeoutId && !leading) {
|
if (!maxTimeoutId && !leading) {
|
||||||
lastCalled = stamp;
|
lastCalled = stamp;
|
||||||
}
|
}
|
||||||
var remaining = maxWait - (stamp - lastCalled);
|
var remaining = maxWait - (stamp - lastCalled);
|
||||||
if (remaining <= 0) {
|
if (remaining <= 0) {
|
||||||
clearTimeout(maxTimeoutId);
|
if (maxTimeoutId) {
|
||||||
maxTimeoutId = null;
|
clearTimeout(maxTimeoutId);
|
||||||
|
maxTimeoutId = null;
|
||||||
|
}
|
||||||
lastCalled = stamp;
|
lastCalled = stamp;
|
||||||
result = func.apply(thisArg, args);
|
result = func.apply(thisArg, args);
|
||||||
}
|
}
|
||||||
@@ -5448,7 +5452,7 @@
|
|||||||
maxTimeoutId = setTimeout(maxDelayed, remaining);
|
maxTimeoutId = setTimeout(maxDelayed, remaining);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (wait !== maxWait) {
|
if (!timeoutId && wait !== maxWait) {
|
||||||
timeoutId = setTimeout(delayed, wait);
|
timeoutId = setTimeout(delayed, wait);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user