Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Trying To Figure Out Josh W. Comeau's Debouncer In An Event Listener

I'm using Josh's debouncer script snippet. I've got it working using a `const doUpdate1 = debounce((event) =< {}, 100)`` approach, but can't figure out how to do it if the function is defined with `function``.

Details

Josh's script looks like this:

JavaScript

function debounce(callback, wait) {
  console.log("- in debounce")
  let timeoutId = null;
  return (...args) => {
    window.clearTimeout(timeoutId);
    timeoutId = window.setTimeout(() => {
      callback.apply(null, args);
    }, wait);
  };
}

Given these two buttons:

HTML

<div class="buttons">
<button class="clicker1">Clicker 1</button>
<button class="clicker2">Clicker 2</button>
</div>

This is breaking my understanding of javascript a little. I thought using `return`` in `doUpdate2`` would produce the same result.

I'm not sure what's going on here. I'm over on Mastodon if you've got suggestions.

References