Cleanup _.debounce.

Former-commit-id: 0212c6b31222a8e215d6f60e906fbad074f424a9
This commit is contained in:
John-David Dalton
2013-05-02 00:06:55 -07:00
parent e773efdc59
commit 9ae24141a3

View File

@@ -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;