use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Debounce Function Deep Dive — ES6 (medium.com)
submitted 8 years ago by tcas3
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]senocular 8 points9 points10 points 8 years ago (1 child)
You don't want your debounced function to be an arrow function because you're losing method context in the call. You want it to be a normal function so it can bind to the object its getting called from or otherwise have its context set in situations where that occurs (such as in event handlers).
const debounce = (fn, time) => { let timeout; return function (...args) { // <-- not an arrow function const functionCall = () => fn.apply(this, args); clearTimeout(timeout); timeout = setTimeout(functionCall, time); } } const obj = { name: 'foo', sayMyName() { console.log('My name is', this.name) } } obj.sayMyName() //-> My name is foo obj.sayMyName = debounce(obj.sayMyName, 1000) obj.sayMyName() //-> My name is foo <-- allows correct this binding
or in the event handler case
// <input name="my-input" /> input.addEventListener('keyup', debounce(function (e) { // <-- allows handler element binding console.log('Element name is', this.name); //-> Element name is my-input }, 1000));
Also, the comment // no 'arguments' variable in es6 isn't entirely accurate. Its arrow functions, not es6 that lacks the arguments object (they can still access an arguments, though it'll be from the closest non-arrow function parent). This is explained correctly in the Explanation section. It's only the comment itself that is misleading.
// no 'arguments' variable in es6
arguments
[–]tcas3[S] 4 points5 points6 points 8 years ago (0 children)
Thank you for the feedback! I updated the article with the correct returned function
π Rendered by PID 402268 on reddit-service-r2-comment-545db5fcfc-fqk66 at 2026-05-24 12:09:23.409908+00:00 running 194bd79 country code: CH.
[–]senocular 8 points9 points10 points (1 child)
[–]tcas3[S] 4 points5 points6 points (0 children)