The past is unbearable
I remember a long time ago in an interview, I was having a nice talk with the technology brother, and he suddenly said: talk about shaking and throttling.
Wipe a cool sweat, I answered with trembling, not confident tone: er, tremble is should be to prevent the meaning of shaking, throttling is probably to save the flow…
At that time, my mental activity is this:
When the psychological quality is really bad, this hurdle did not turn over the past, the final result is:
Shame, then courage
With my unyielding character, this game must be recovered!
After years of hard work and training, I am now extremely confident. If I could go back in time, I believe I could do this:
Today, I want to make a final decision with “throttling” to prove that I am not what I used to be, to use an idiom to describe it, that is – not what I used to be!
If you know yourself and your enemy, you will win a hundred battles
The old law tells us that in order to destroy an enemy we must first know him.
Anti-skid (Debounce)
From a literal point of view, shiver prevention means to prevent shaking.
For example, let’s say your date asks you to take a beautiful photo of her, but you work too much overtime, your body is weak, your hands are shaking, and the picture is blurred. You need to do something to avoid getting angry. First, let’s analyze why the photo is blurry:
- The hand shakes badly (1s more than ten times!! ??
- As a result, the camera cannot capture the picture perfectly
- So the photo is blurry.
It seems that hand shaking is the main culprit, so find a way to solve the problem: find a stand that supports the camera instead of your hand, so that the shaking stops!
This operation, I have to give you a thumbs up:
So let’s think about what problem does this example essentially solve?
I think it solves the problem of “shaking frequency” — holding the hand, shaking many times a second; Can stand to support, good long time all may not shake once. It can be said that “people are not as good as” ah!
That’s what the bracket does.
To get back to our topic today, let’s take a look at the general explanation of shaking stabilization: when a function is called continuously, the function is not executed, only once when all calls have stopped for a certain amount of time.
Throttle
Now look back at my answer in those days, or touched a threshold (manual relief embarrassment, hey hey).
Another example: a faucet at home, no matter how big you turn it on, can only produce that much water per unit of time, because the size of the faucet limits the amount of water it can flow per unit of time.
The faucet’s job is to throttle.
If an event is triggered multiple times in a unit time, only one time will be effective.
Common scenario
The anti-shake throttling is generally used in the following scenarios:
- The zooming of the browser window is triggered by the slide of the page
resize
,scroll
- Mouse events
onmousemove
,onmousedown
.onmouseup
- Input box input triggered
keyup
,keydown
…
The principle of
Anti – shaking maintains a timer that fires after a delay, but if it fires again during a delay, the current timer will be cleared and the timeout call will be reset. This way, only the last operation can be triggered.
Throttling fires the function by determining whether a certain time has been reached. If not, a timer is used to delay it, and the next event resets the timer.
The difference between
Function buffering is executed only once in a certain period of time, while function throttling is executed at intervals, ensuring that the actual event handler is executed once within a specified period of time, no matter how frequently the event is fired.
conclusion
Today, I’ll start by looking at the basics of “stabilization” and “throttling”, and the detailed code implementation will come later.
~
~
~ to be continued
Learn interesting knowledge, make interesting friends, and create interesting souls!
Everybody is good! I am the author of “programming Samadhi” hermit King, my public number is “programming Samadhi”, welcome to pay attention, I hope you can give me more advice!
Both knowledge and skill, internal force and external work, both theory and practice should grasp, both hands should be hard!