Jhey Tompkins
1 min readDec 12, 2017

--

Hey ricace 👋

Thanks for your response! Let me try and help out 😃

I will update the code snippets as maybe the return statements are misleading or confusing.

The debounce does indeed use a closure to maintain an internal reference to inDebounce that is of type number or undefined. The returned function doesn’t need to return a value so changing from return inDebounce = ... to simply inDebounce = ... won’t change the behaviour. Note that all functions return a value in JavaScript, in this case, it will be undefined. The most important thing is that any previous setTimeout is cleared and that a new one is created that will invoke the passed in function.

clearTimeout is always invoked. Regardless of whether inDebounce is defined.

Lastly, your second code snippet would not work as intended for various reasons but the main one being that there is no reference being maintained for inDebounce. If you put together a demo and log the internal values in the invocation this may help to understand how the debounce is working. I will put together a demo and attach it to the post 👍 I’ll also update the code snippets.

UPDATE:: As promised, here is a demo of the throttle and debounce in action! 😎

Hope that helps!

--

--

Jhey Tompkins
Jhey Tompkins

Written by Jhey Tompkins

I make awesome things for awesome people!

Responses (1)