We successfully implemented our Dearborn's function, but you might also get a question how to implement
throttle function.
And actually, they're super similar.
And this is why I have the code from out the bones function because we really need to change like one
percent of the code.
So the idea of throttle function is that it will be executed immediately and just to remind you the
bones function will be executed only after our timeout here.
Throttle function executes it immediately.
But after that, the calling of the function is completely ignored for our timeout, which actually
means if a user is typing something, we can call our function only at the beginning and then every
timeout.
So every two seconds, for example.
And this is exactly the core purpose of throttle.
And to simulate it, we must change our code a little bit.
So here we have our saving, but this is fine here this process change.
But we don't want to use here the bones.
We want to write it with throttle.
So the arguments are exactly the same.
And now here we call process change.
At the beginning, this is totally fine, but now I want to simulate that we're calling it with a timeout.
So here we are, right and set timeout, and we want to call this function process change inside it
after some time, for example, after one second, then I will copy paste this set timeout and run it
after one second and 200 milliseconds.
Now I copy paste it again and I will call this function again after two seconds and 400 milliseconds,
and then I will remove to last process change.
So actually, as you can see here, we put timeout 200 milliseconds, which actually means this function
must be executed immediately.
This timeout will be ignored.
This will be also ignored and done with the last two.
Timeout will call this function because it will happen after this timeout of 200 milliseconds.
Now, first of all, let's change the name of our to bonds to throttle where you get and hear the same
arguments of function and our timeout.
But now here we need not a timer ID, but a Boolean, and this boolean will show us if we can execute
this function again.
This is why not let timeout but lead, for example, is waiting and by default it is false and no here,
but also return a function.
We don't need a clear timeout and actually set timeout is staying here, but we don't need timeout because
actually we will forbid this set timeout within this condition.
So here we can write.
If not is waiting, then we want to call this set timeout.
In other case, we won't call anything in this code will be just ignored.
Also, it is important that we call this function directly.
This is why I want to move it inside out.
If condition and inside that timeout, we simply change our is rate in property to false back.
So as you can see, this line means that at the beginning on the first call, we will call this function.
But after this, we want to set our flag to true.
This is why here were writing is Vaden equals true.
Then we have a set time, for example, two seconds and after two seconds where Satan is waiting to
false, which actually means we can call this function on the after two seconds and not before.
So as you can see, the code is 99 percent the same.
The main idea is that we're building here is wait and flag, and we just allow to accept this function
once in our timeout.
Now let's check if it's working.
I'm reloading the page.
We're getting directly saving input for first time and then we see onlyone save input for second time.
And this is exactly this code process change.
And this last timeout was two seconds and 400 milliseconds because actually here on our first try,
we're going to throttle is waiting is false.
We're going here, we're checking the key.
It is not true.
Then we're calling directly this function and we're setting our flag to true.
And then after our set timeout and then we're changing this weight and back to false and we don't need
here any time already because we never abort our set timeout.
And the next question that you might get, OK, but what is the difference between the bones and throttle?
And there's the other two said, were Colin de Bond's after use a stop to do something and work on and
throttle for us to follow the beginning.
And then after a certain interval of time.