diff --git a/src/compat/function/debounce.spec.ts b/src/compat/function/debounce.spec.ts index 0817a5554..2042bd37a 100644 --- a/src/compat/function/debounce.spec.ts +++ b/src/compat/function/debounce.spec.ts @@ -559,6 +559,30 @@ describe('debounce', () => { expect(callCount).toBe(0); }); + it('should not invoke the function with `maxWait` when both `leading` and `trailing` are `false`', async () => { + let callCount = 0; + + const debounced = debounce( + () => { + callCount++; + }, + 64, + { leading: false, trailing: false, maxWait: 64 } + ); + + debounced(); + debounced(); + + await delay(96); + + debounced(); + debounced(); + + await delay(96); + + expect(callCount).toBe(0); + }); + it('should match the type of lodash', () => { expectTypeOf(debounce).toEqualTypeOf(); }); diff --git a/src/compat/function/debounce.ts b/src/compat/function/debounce.ts index 559985b3b..b8248b5dd 100644 --- a/src/compat/function/debounce.ts +++ b/src/compat/function/debounce.ts @@ -198,7 +198,10 @@ export function debounce any>( } if (Date.now() - pendingAt >= maxWait) { - result = func.apply(this, args); + if (leading || trailing) { + result = func.apply(this, args); + } + pendingAt = Date.now(); _debounced.cancel(); diff --git a/src/compat/function/throttle.spec.ts b/src/compat/function/throttle.spec.ts index 93d05ceb3..41914e587 100644 --- a/src/compat/function/throttle.spec.ts +++ b/src/compat/function/throttle.spec.ts @@ -353,6 +353,30 @@ describe('throttle', () => { expectTypeOf(throttle).toEqualTypeOf(); }); + it('should not invoke the function when both `leading` and `trailing` are `false`', async () => { + let callCount = 0; + + const throttled = throttle( + () => { + callCount++; + }, + 64, + { leading: false, trailing: false } + ); + + throttled(); + throttled(); + + await delay(96); + + throttled(); + throttled(); + + await delay(96); + + expect(callCount).toBe(0); + }); + it('should not invoke the function even after flush is called if timer is going', async () => { let callCount = 0; const throttled = throttle(() => ++callCount, 32);