From 4f92fb7007846e4951374d37c253dfcbacfc0b18 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 28 Mar 2016 21:06:11 -0700 Subject: [PATCH] Ensure `cancel` resets `lastCallTime` and `lastInvokeTime`. --- lodash.js | 18 +++++++++++------- test/test.js | 4 +++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lodash.js b/lodash.js index 34654d26d..2944a4288 100644 --- a/lodash.js +++ b/lodash.js @@ -9330,6 +9330,7 @@ if (timerId !== undefined) { clearTimeout(timerId); } + lastCallTime = lastInvokeTime = 0; lastArgs = lastThis = timerId = undefined; } @@ -9338,17 +9339,20 @@ } function debounced() { + var time = now(), + isInvoking = shouldInvoke(time); + lastArgs = arguments; lastThis = this; - lastCallTime = now(); + lastCallTime = time; - if (timerId === undefined) { - return leadingEdge(lastCallTime); - } - // Check times to handle invocations in a tight loop. - if (shouldInvoke(lastCallTime)) { + if (isInvoking) { + if (timerId === undefined) { + return leadingEdge(lastCallTime); + } + // Handle invocations in a tight loop. clearTimeout(timerId); - timerId = undefined; + timerId = setTimeout(timerExpired, wait); return invokeFunc(lastCallTime); } return result; diff --git a/test/test.js b/test/test.js index c4765ba56..bf46ddb82 100644 --- a/test/test.js +++ b/test/test.js @@ -21956,10 +21956,12 @@ assert.strictEqual(funced(), 1); funced.cancel(); + assert.strictEqual(funced(), 2); + funced(); setTimeout(function() { - assert.strictEqual(callCount, 2); + assert.strictEqual(callCount, 3); done(); }, 64); });