From 9ae24141a3e166b2c4ad1f179a13293c2acff707 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 2 May 2013 00:06:55 -0700 Subject: [PATCH] Cleanup `_.debounce`. Former-commit-id: 0212c6b31222a8e215d6f60e906fbad074f424a9 --- lodash.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lodash.js b/lodash.js index 99435a94b..a111b5c41 100644 --- a/lodash.js +++ b/lodash.js @@ -4482,17 +4482,16 @@ */ function debounce(func, wait, options) { var args, - calledTwice, - inited, result, thisArg, timeoutId, + callCount = 0, trailing = true; function delayed() { - var callTrailing = trailing && ((leading && calledTwice) || !leading); - calledTwice = inited = timeoutId = null; - if (callTrailing) { + var isCalled = trailing && (!leading || callCount > 1); + callCount = timeoutId = 0; + if (isCalled) { result = func.apply(thisArg, args); } } @@ -4508,11 +4507,8 @@ thisArg = this; clearTimeout(timeoutId); - if (!inited && leading) { - inited = true; + if (leading && ++callCount < 2) { result = func.apply(thisArg, args); - } else { - calledTwice = true; } timeoutId = setTimeout(delayed, wait); return result;