Question: I got this function:

```js
const debounce = (func, delay) => {
  let inDebounce;

  return function() {
    const context = this
    const args = arguments
    clearTimeout(inDebounce)
    inDebounce = setTimeout(() => func.apply(context, args), delay)
  }
}

var likes_btn = document.getElementsByClassName("js-submit-like");

for (var i = 0; i < likes_btn.length; i++) {
  likes_btn[i].addEventListener('click', debounce(function(el) {
    alert("hello");

    el.preventDefault();
  }, 500));
}
```

So the thing is, that I need to use the `.preventDefault()` before the debounce gets executed. Currently, what really happens is that is executed at the end of `debounce()`, not into the function scope.

How can I acces into the function scope? Thanks!

Answer: 
Create a separate `debouncer` callback that the listener can close over:

```js
const debounce = (func, delay) => {
  let inDebounce;

  return function() {
    const context = this
    const args = arguments
    clearTimeout(inDebounce)
    inDebounce = setTimeout(() => func.apply(context, args), delay)
  }
}

var likes_btn = document.getElementsByClassName("js-submit-like");

for (let i = 0; i < likes_btn.length; i++) {
  const button = likes_btn[i];
  const debouncer = debounce((e) => console.log("Hello", button), 500); // you can also use e.target to refer to the button
  button.addEventListener('click', function(e) {
    console.log("clicked");
    debouncer(e);
    e.preventDefault();
  });
}
```

```html
<a class="js-submit-like" href="http://www.facebook.com/">Like</a>

<a class="js-submit-like" href="http://www.facebook.com/">Like 2</a>
```
Just move it outside of the debouncer callback:

```
const debouncer = debounce(() => alert("hello"), 500);
likes_btn[i].addEventListener('click', function(event) {
   event.preventDefault();
   debouncer(event);
});

```

That might be a bit more elegant with some chained functions:

```
 const both = (fn1, fn2) => (...args) => (fn1(...args), fn2(...args));
 const stop = event => event.preventDefault();
 const listen = (el, name, handler) => el.addEventListener(name, handler);

 for(const btn of likes_btn) {
   listen(btn, "click", both(
    debounce(() => alert("hello"), 500),
    stop
   ));
}

```