mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Cleanup _.debounce.
Former-commit-id: 0212c6b31222a8e215d6f60e906fbad074f424a9
This commit is contained in:
14
lodash.js
14
lodash.js
@@ -4482,17 +4482,16 @@
|
|||||||
*/
|
*/
|
||||||
function debounce(func, wait, options) {
|
function debounce(func, wait, options) {
|
||||||
var args,
|
var args,
|
||||||
calledTwice,
|
|
||||||
inited,
|
|
||||||
result,
|
result,
|
||||||
thisArg,
|
thisArg,
|
||||||
timeoutId,
|
timeoutId,
|
||||||
|
callCount = 0,
|
||||||
trailing = true;
|
trailing = true;
|
||||||
|
|
||||||
function delayed() {
|
function delayed() {
|
||||||
var callTrailing = trailing && ((leading && calledTwice) || !leading);
|
var isCalled = trailing && (!leading || callCount > 1);
|
||||||
calledTwice = inited = timeoutId = null;
|
callCount = timeoutId = 0;
|
||||||
if (callTrailing) {
|
if (isCalled) {
|
||||||
result = func.apply(thisArg, args);
|
result = func.apply(thisArg, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4508,11 +4507,8 @@
|
|||||||
thisArg = this;
|
thisArg = this;
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
if (!inited && leading) {
|
if (leading && ++callCount < 2) {
|
||||||
inited = true;
|
|
||||||
result = func.apply(thisArg, args);
|
result = func.apply(thisArg, args);
|
||||||
} else {
|
|
||||||
calledTwice = true;
|
|
||||||
}
|
}
|
||||||
timeoutId = setTimeout(delayed, wait);
|
timeoutId = setTimeout(delayed, wait);
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user