Jhey Tompkins
1 min readDec 13, 2017

--

Hey ricace 👋

Yes in a sense. debounce is a convenience function that you’d likely import where necessary from a module. This is why we pass in a function along with a desired delay as parameters.

Whilst theoretically you could implement it as you say by just passing the inner function to your event listener, you lose the value that is gained from defining the debounce function.

It’s not faking a global variable. It’s declaring a local variable. This saves you having to declare separate global variables for each inner function you would implement. This would need to be done so that you would be able to run more than one debounce function at the same time, else they will each want to use the same global variable. This isn’t necessary with the debounce function as each instance used has it’s own scoped local variable(definitely recommend tinkering with this in the demo to see this in action using console.info) Lastly, you would also need to declare a different inner function each time you wanted to change the delay.

I do mention the similarities between throttle and debounce in the post. Although their implementations do differ slightly, throttle is essentially like a chaining debounce.

Hope that helps!

--

--

Jhey Tompkins
Jhey Tompkins

Written by Jhey Tompkins

I make awesome things for awesome people!

No responses yet