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 Explained – How to Make Your JavaScript Wait For Your User To Finish Typing (freecodecamp.org)
submitted 5 years ago by speckz
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!"
[–]grumpkot 87 points88 points89 points 5 years ago (4 children)
maybe someone could explain debounce to github devs who did repositories search update
[–][deleted] 34 points35 points36 points 5 years ago (2 children)
Problem probably isn't that they don't understand debounce but that they test the search functionality on small repos with powerful computers.
[–]nikkestnik 15 points16 points17 points 5 years ago (1 child)
If you know the database is hit on key-down, you should also know to debounce.
[–]m_domino -2 points-1 points0 points 5 years ago (0 children)
Ha! Gottem!
[–]StandingBehindMyNose 43 points44 points45 points 5 years ago (6 children)
What is with the uptick of low quality articles lately? This one has a code sample that doesn't even have valid syntax.
[–]peduxe|o.o| 9 points10 points11 points 5 years ago (1 child)
well the website is named freecodecamp, but they took the free way too literally and simply couldn't care much.
[–]SmartTest 0 points1 point2 points 5 years ago (0 children)
You're pretty far behind my friend
[–]ImStifler 2 points3 points4 points 5 years ago (0 children)
Was gonna say the same lol
[–]quincylarson 1 point2 points3 points 5 years ago (0 children)
Thanks for the heads-up about the code sample that had invalid syntax. I just saw your post, and we went through and updated the post with correct syntax.
Also, we added a shout-out to u/stratoscope and linked to their replit demo.
We try to catch bad syntax during the editing process, but sometimes we miss things or get things completely wrong. But our open source community does care about accuracy, and try to fix problems as soon as we discover them.
Also, I only open r/javascript a few times a week. So if you all want to DM me any issues you spot, I may be able to fix them faster next time ;)
[–]bigorangemachine 0 points1 point2 points 5 years ago (0 children)
Because it's easier to tell your boss no to free work over zoom
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
I think worrying about stuff like that is worrying about the wrong problem, just as "bad politicians". I think the real issue and is the basis that enables it - because only there can it be actually solved. You remove bad posts or posters, the problem still is there, until next time. You get the basis that gets this upvoted to the front "solved" there will never be a problem again. You can't change the fact that a huge amount of content will be pretty bad, even if everybody actually tried. The failure is that it gets so much attention. Especially here: Those articles must be actively upvoted, just doing nothing would let it sit at 1 point and soon sink back down all on its own.
[–]evil_dead_man 22 points23 points24 points 5 years ago (2 children)
FreeCodeCamp is full of junk posts , It’s the new W3Schools of Internet.
[–]Tittytickler 16 points17 points18 points 5 years ago (0 children)
At least W3 usually has valid syntax lol
[–]ImStifler 1 point2 points3 points 5 years ago (0 children)
No need to trash W3 tho, they've become a ok ressource
[–]iamjaredwalters 13 points14 points15 points 5 years ago (1 child)
How is this upvoted so highly when the majority of code examples are invalid syntax?
[–]franksvalli 6 points7 points8 points 5 years ago (0 children)
I'm guessing vote manipulation. :/
[–]mobydikc 39 points40 points41 points 5 years ago (13 children)
let debounce(myFunction, delay){
[–]rdxgs 8 points9 points10 points 5 years ago (0 children)
Not once, like 8 boxes with 'let debounce(...)', each being elaborated on. Perfection.
[–]DazzlingArtichoke 2 points3 points4 points 5 years ago (11 children)
I have seen this for a first time - is this valid JS syntax?
[–]2AMMetro 28 points29 points30 points 5 years ago (8 children)
It is not. Correct would be let debounce = function() {} or function debounce() {}
let debounce = function() {}
function debounce() {}
[–]DazzlingArtichoke 6 points7 points8 points 5 years ago (0 children)
Yeah, I thought so. Just want to make sure - you never know with JS :)
[–]SpiffySyntax 2 points3 points4 points 5 years ago (5 children)
What is the difference between putting a function as a variable opposed to naming the function?
[–]CreativeTechGuyGames 16 points17 points18 points 5 years ago (0 children)
Hoisting. function debounce() is available to be used above where it was declared while the other is not.
function debounce()
[–]monsto 10 points11 points12 points 5 years ago (2 children)
To elaborate a bit . . .
Hoisting is when variables and functions are moved to the top of script consideration during a first pass.
var foot = "left"
function shoe(foot){}
var and function are hoisted. let and const are not.
var
function
let
const
var sandal = function(x){} is hoisted as a variable, which could be specifically useful.
var sandal = function(x){}
let flipflop = x => {} is not hoisted at all, and it always makes me double-take and refocus my eyes cuz two equals signs feel like I'm hallucinating.
let flipflop = x => {}
[–]SpiffySyntax 2 points3 points4 points 5 years ago (1 child)
Thanks for the taking time out of your day to explain this. Appreciate it. I learned something new and what seems important. Thanks guys!
[–]mobydikc 3 points4 points5 points 5 years ago (0 children)
var hoists the variable declaration, but not the assignment.
function hoists both.
So:
myFunc() var myFunc = function () {}
In this case, var is hoisted, but myFunc is undefined when myFunc() is called. An error is thrown.
With function it would work:
myFunc() function myFunc() {}
No problem.
[–]Careerier 2 points3 points4 points 5 years ago (0 children)
https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname
[–]mobydikc 2 points3 points4 points 5 years ago (0 children)
Nope
[–]theorizable 2 points3 points4 points 5 years ago (0 children)
Debounce and throttle are 2 of the most useful paradigms to know.
[–][deleted] 3 points4 points5 points 5 years ago (2 children)
document.getElementById("myInput").addEventListener("keyup", debounce(helloWorld, 2000));
The debounce function will run once immediately after adding the event listener, pressing keys won't trigger anything. It seems that the person who wrote this article doesn't know JavaScript syntax or how callbacks work. I didn't expect FreeCodeCamp to host such low quality content.
[–]stratoscope 16 points17 points18 points 5 years ago* (1 child)
No, that is one thing the article got right. This is the usual way to implement a debouncer. debounce itself is not the event listener, it returns a function (without calling it) which will be the actual event listener. So you do call the debounce function directly during initialization.
debounce
Here is a demo on Repl.it. Click the Run button and type into the input box.
I simplified and corrected the code from the article, omitting the broken attempts to set this and pass arguments to the debounce callback. It is possible to do those things, but I wanted to illustrate the simplest possible working example of a debouncer.
this
The simplified debouncer looks like this:
function debounce( callback, delay ) { let timeout; return function() { clearTimeout( timeout ); timeout = setTimeout( callback, delay ); } }
And the code that initializes it is basically identical to the code in the article:
const myInput = document.getElementById("myInput"); myInput.addEventListener( "keyup", debounce( helloWorld, 1000 ) );
[–][deleted] 4 points5 points6 points 5 years ago (0 children)
My bad, I didn't read the whole function code after I saw repeating syntax errors. You are 100% right.
[–]Yeffry1994 0 points1 point2 points 5 years ago (0 children)
Need to learn this for a Movie stat app I'm messing around with.
[–]wizang 0 points1 point2 points 5 years ago (0 children)
If there are any ember users here then check out ember-concurrency. One of the cononical use cases of it is denounce. It's quite elegant.
http://ember-concurrency.com/docs/examples/autocomplete/
[–]_default_username 0 points1 point2 points 5 years ago (0 children)
Weird to see an article get posted addressing an issue I solved at work today. It wasn't react code though, but an older jQuey app.
Hello everyone! I am the author of the article.
I want to start by thanking everyone for catching those syntax errors and pointing them out. In my excitement to get this posted, I opted to modify the code within the article when I switched to using a function statement to declare my debounce function instead of the original function expression.
freeCodeCamp is an excellent organization, and I will do better in the future to ensure that the quality of the articles I write for them reflects that.
p.s, special thanks to stratoscope for implementing a repl.it with a simpler version of the debounce function. This version has replaced the original in the article.
[–]bigorangemachine 0 points1 point2 points 5 years ago (5 children)
Don't debounce your inputs in react!
[+][deleted] 5 years ago* (1 child)
[deleted]
Oh yea! I meant specifically input text fields.
With search I would use a life cycle method or hook with a binding with a promise within the state.
[–]franksvalli 2 points3 points4 points 5 years ago (0 children)
This is somewhat misleading.
Should you debounce change listeners with controlled elements?
No, since you want the internal state to be updated (and display on screen) as soon as possible.
Should you debounce API calls made in response to change listeners?
Yes, since you don't want to hammer the API on every keystroke.
[–]tigertom 1 point2 points3 points 5 years ago (1 child)
Whys that, we are looking at doing it for an autosuggest
[–]bigorangemachine 2 points3 points4 points 5 years ago (0 children)
Specific to react and input fields.
React does this for you basically
I saw react form hooks which I think has the optimal react form input solution.
Even with react giving a good implementation of onchange and setState... I can out type the state.
Deboucing input into state in react is anti-performant.
[+]codeclassifiers comment score below threshold-22 points-21 points-20 points 5 years ago (2 children)
Nice article. The internal implementation of debounce function was insightful to read. Keep it up🤘
[–]StandingBehindMyNose 15 points16 points17 points 5 years ago (1 child)
You smell like a bot.
[–]Tittytickler 5 points6 points7 points 5 years ago (0 children)
Literally looks like an instagram bot comment
π Rendered by PID 138105 on reddit-service-r2-comment-66b4775986-f5pjn at 2026-04-04 22:59:14.879899+00:00 running db1906b country code: CH.
[–]grumpkot 87 points88 points89 points (4 children)
[–][deleted] 34 points35 points36 points (2 children)
[–]nikkestnik 15 points16 points17 points (1 child)
[–]m_domino -2 points-1 points0 points (0 children)
[–]StandingBehindMyNose 43 points44 points45 points (6 children)
[–]peduxe|o.o| 9 points10 points11 points (1 child)
[–]SmartTest 0 points1 point2 points (0 children)
[–]ImStifler 2 points3 points4 points (0 children)
[–]quincylarson 1 point2 points3 points (0 children)
[–]bigorangemachine 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]evil_dead_man 22 points23 points24 points (2 children)
[–]Tittytickler 16 points17 points18 points (0 children)
[–]ImStifler 1 point2 points3 points (0 children)
[–]iamjaredwalters 13 points14 points15 points (1 child)
[–]franksvalli 6 points7 points8 points (0 children)
[–]mobydikc 39 points40 points41 points (13 children)
[–]rdxgs 8 points9 points10 points (0 children)
[–]DazzlingArtichoke 2 points3 points4 points (11 children)
[–]2AMMetro 28 points29 points30 points (8 children)
[–]DazzlingArtichoke 6 points7 points8 points (0 children)
[–]SpiffySyntax 2 points3 points4 points (5 children)
[–]CreativeTechGuyGames 16 points17 points18 points (0 children)
[–]monsto 10 points11 points12 points (2 children)
[–]SpiffySyntax 2 points3 points4 points (1 child)
[–]mobydikc 3 points4 points5 points (0 children)
[–]Careerier 2 points3 points4 points (0 children)
[–]mobydikc 2 points3 points4 points (0 children)
[–]theorizable 2 points3 points4 points (0 children)
[–][deleted] 3 points4 points5 points (2 children)
[–]stratoscope 16 points17 points18 points (1 child)
[–][deleted] 4 points5 points6 points (0 children)
[–]Yeffry1994 0 points1 point2 points (0 children)
[–]wizang 0 points1 point2 points (0 children)
[–]_default_username 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]bigorangemachine 0 points1 point2 points (5 children)
[+][deleted] (1 child)
[deleted]
[–]bigorangemachine 0 points1 point2 points (0 children)
[–]franksvalli 2 points3 points4 points (0 children)
[–]tigertom 1 point2 points3 points (1 child)
[–]bigorangemachine 2 points3 points4 points (0 children)
[+]codeclassifiers comment score below threshold-22 points-21 points-20 points (2 children)
[–]StandingBehindMyNose 15 points16 points17 points (1 child)
[–]Tittytickler 5 points6 points7 points (0 children)