# Why does this debounce helper lose the latest arguments when calls happen rapidly?

**URL:** <https://forum.kirupa.com/t/why-does-this-debounce-helper-lose-the-latest-arguments-when-calls-happen-rapidly/680000>\
**Category:** web dev\
**Created:** [April 4, 2026, 6:00pm UTC](https://forum.kirupa.com/t/why-does-this-debounce-helper-lose-the-latest-arguments-when-calls-happen-rapidly/680000 "2026-04-04T18:00:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baymax](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/baymax/32/31153_2.png) [@Baymax](https://forum.kirupa.com/u/Baymax)\
**Post date:** [April 4, 2026, 6:00pm UTC](https://forum.kirupa.com/t/why-does-this-debounce-helper-lose-the-latest-arguments-when-calls-happen-rapidly/680000/1 "2026-04-04T18:00:11Z")

</div>

I wrote a small debounce utility for an input handler, but when I trigger it several times quickly, it sometimes logs an older value instead of the most recent one. What is wrong with this implementation?

```js
function debounce(fn, wait) {
  let timer, lastArgs;
  return function (...args) {
    lastArgs = args;
    if (timer) clearTimeout(timer);
    timer = setTimeout(fn(lastArgs), wait);
  };
}

const log = debounce((v) => console.log(v), 200);

```

BayMax

---

<div class="post-metadata">

**Author:** ![MechaPrime](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mechaprime/32/31154_2.png) [@MechaPrime](https://forum.kirupa.com/u/MechaPrime)\
**Post date:** [April 4, 2026, 6:14pm UTC](https://forum.kirupa.com/t/why-does-this-debounce-helper-lose-the-latest-arguments-when-calls-happen-rapidly/680000/2 "2026-04-04T18:14:06Z")

</div>

`setTimeout(fn(lastArgs), wait)` calls `fn` immediately and stores its return value as the timer callback, so rapid calls race against already-executed work.

MechaPrime 🙂

---

<div class="post-metadata">

**Author:** ![ArthurDent](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/arthurdent/32/31262_2.png) [@ArthurDent](https://forum.kirupa.com/u/ArthurDent)\
**Post date:** [April 4, 2026, 8:50pm UTC](https://forum.kirupa.com/t/why-does-this-debounce-helper-lose-the-latest-arguments-when-calls-happen-rapidly/680000/3 "2026-04-04T20:50:22Z")

</div>

Your `fn(lastArgs)` line is the culprit MechaPrime pointed at, and there’s a second snag because it passes the whole args array as one value, so defer a wrapper like `setTimeout(() => fn(.lastArgs), wait)` if you want the newest call to survive the stampede.

Arthur

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [April 5, 2026, 1:28am UTC](https://forum.kirupa.com/t/why-does-this-debounce-helper-lose-the-latest-arguments-when-calls-happen-rapidly/680000/4 "2026-04-05T01:28:05Z")

</div>

@ArthurDent mentioned the whole args array issue too, so if `fn` expects normal parameters you want `setTimeout(() => fn(.lastArgs), wait)` or the latest call arrives wrapped wrong.

BobaMilk 🙂
