简体   繁体   中英

Can someone explain me the flow of functions in this example?

多个回调

I looked at the above piece of code and tried my best to search for solutions and posted it here after giving it my all. This is my current understanding of the code: debounce() is called when there is an input and onInput() is passed to it as a callback, and debounce function return another function, the function being returned takes an argument which is the function passed by the debounce() aka the onInput(), I am stuck @ func.apply(null, args); 1.Isn't func and args the same thing???? Someone please explain step by step is possible..

This is an example of debounce.

  • Debouncing is a practice which is used to improve browser performance.
  • A programming practice which ensure that time-consuming tasks do not fire so often.
  • It is used to limits the rate at which a function gets invoked.

I have explained debounce with example, please check out the link debounce

debouce is only called once on the initial run, it creates and returns a new anonymous function - the actual event handler.

When the input event is triggered, the previously created function is executed and will call func (onInput) after 500ms. func is only once passed to debounce, but args are the actual input event arguments, which will be passed on to func via apply . In this case, apply is basically the same as func(...args); So func (aka onInput) will be called with the actual arguments from the input event after 500ms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM