Function throttling, anti-shake function, essential for interview/excellent code

Function throttling and anti-shake functions are two different starting points, but the methodology is the same.

Function throttling : throttling is to reduce unnecessary output, which is to prevent a function from being executed, and the same function starts to execute again, resulting in a waste of performance.
Anti-shake function : execute multiple functions in a short period of time, and they will be refreshed and displayed together after they are all executed, resulting in page jitter.

Common scenarios: Repeatedly click on the query list, keyword search, screen size change monitoring function, etc.

eg: The
screen size change is a dragging process, the function will be executed over many times, in fact, you only want to be meaningful a few times, how can it be meaningful, in this scenario, it is usually a few seconds after the end of the behavior Time is meaningful. Then you can set a timer.

Solution

It's all the same, let's make small adjustments for different scenes

Pseudo code:
{ clear timer time this.time= timer ({callback function}, time) }


Guess you like

Origin blog.csdn.net/weixin_45629623/article/details/115071334
Recommended