From e60e97c03fda22d4dfea84e07022183990cf20df Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 10 Apr 2016 19:53:34 -0700 Subject: [PATCH] Ensure `_.debounce` queues a trailing call after `maxWait`. [closes #2229] --- lodash.js | 3 +++ test/test.js | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/lodash.js b/lodash.js index 4c2315455..bac9c7544 100644 --- a/lodash.js +++ b/lodash.js @@ -9436,6 +9436,9 @@ timerId = setTimeout(timerExpired, wait); return invokeFunc(lastCallTime); } + if (timerId === undefined) { + timerId = setTimeout(timerExpired, wait); + } return result; } debounced.cancel = cancel; diff --git a/test/test.js b/test/test.js index 273e8bc19..4a1866d19 100644 --- a/test/test.js +++ b/test/test.js @@ -4248,6 +4248,29 @@ }, 1); }); + QUnit.test('should queue a trailing call for subsequent debounced calls after `maxWait`', function(assert) { + assert.expect(1); + + var done = assert.async(); + + var callCount = 0; + + var debounced = _.debounce(function() { + ++callCount; + }, 64, { 'maxWait': 64 }); + + debounced(); + + lodashStable.times(20, function(index) { + setTimeout(debounced, 54 + index); + }); + + setTimeout(function() { + assert.strictEqual(callCount, 2); + done(); + }, 160); + }); + QUnit.test('should cancel `maxDelayed` when `delayed` is invoked', function(assert) { assert.expect(2);