1
00:00:00,330 --> 00:00:05,780
We successfully implemented our Dearborn's function, but you might also get a question how to implement

2
00:00:05,790 --> 00:00:06,780
throttle function.

3
00:00:10,020 --> 00:00:11,820
And actually, they're super similar.

4
00:00:11,970 --> 00:00:17,550
And this is why I have the code from out the bones function because we really need to change like one

5
00:00:17,550 --> 00:00:18,600
percent of the code.

6
00:00:19,020 --> 00:00:24,840
So the idea of throttle function is that it will be executed immediately and just to remind you the

7
00:00:24,840 --> 00:00:28,680
bones function will be executed only after our timeout here.

8
00:00:28,680 --> 00:00:31,110
Throttle function executes it immediately.

9
00:00:31,350 --> 00:00:37,200
But after that, the calling of the function is completely ignored for our timeout, which actually

10
00:00:37,200 --> 00:00:43,020
means if a user is typing something, we can call our function only at the beginning and then every

11
00:00:43,020 --> 00:00:43,590
timeout.

12
00:00:43,590 --> 00:00:45,330
So every two seconds, for example.

13
00:00:45,540 --> 00:00:48,030
And this is exactly the core purpose of throttle.

14
00:00:48,210 --> 00:00:51,270
And to simulate it, we must change our code a little bit.

15
00:00:51,540 --> 00:00:55,440
So here we have our saving, but this is fine here this process change.

16
00:00:55,620 --> 00:00:57,660
But we don't want to use here the bones.

17
00:00:57,660 --> 00:00:59,520
We want to write it with throttle.

18
00:00:59,730 --> 00:01:01,890
So the arguments are exactly the same.

19
00:01:02,100 --> 00:01:04,110
And now here we call process change.

20
00:01:04,110 --> 00:01:09,930
At the beginning, this is totally fine, but now I want to simulate that we're calling it with a timeout.

21
00:01:10,230 --> 00:01:15,630
So here we are, right and set timeout, and we want to call this function process change inside it

22
00:01:15,900 --> 00:01:21,840
after some time, for example, after one second, then I will copy paste this set timeout and run it

23
00:01:21,840 --> 00:01:24,900
after one second and 200 milliseconds.

24
00:01:25,080 --> 00:01:32,010
Now I copy paste it again and I will call this function again after two seconds and 400 milliseconds,

25
00:01:32,340 --> 00:01:34,950
and then I will remove to last process change.

26
00:01:35,250 --> 00:01:41,670
So actually, as you can see here, we put timeout 200 milliseconds, which actually means this function

27
00:01:41,670 --> 00:01:43,200
must be executed immediately.

28
00:01:43,560 --> 00:01:45,360
This timeout will be ignored.

29
00:01:45,600 --> 00:01:48,480
This will be also ignored and done with the last two.

30
00:01:48,480 --> 00:01:55,050
Timeout will call this function because it will happen after this timeout of 200 milliseconds.

31
00:01:55,350 --> 00:02:00,630
Now, first of all, let's change the name of our to bonds to throttle where you get and hear the same

32
00:02:00,630 --> 00:02:02,940
arguments of function and our timeout.

33
00:02:03,150 --> 00:02:09,180
But now here we need not a timer ID, but a Boolean, and this boolean will show us if we can execute

34
00:02:09,180 --> 00:02:10,230
this function again.

35
00:02:10,500 --> 00:02:17,310
This is why not let timeout but lead, for example, is waiting and by default it is false and no here,

36
00:02:17,310 --> 00:02:18,840
but also return a function.

37
00:02:19,030 --> 00:02:25,620
We don't need a clear timeout and actually set timeout is staying here, but we don't need timeout because

38
00:02:25,620 --> 00:02:29,350
actually we will forbid this set timeout within this condition.

39
00:02:29,370 --> 00:02:30,450
So here we can write.

40
00:02:30,450 --> 00:02:34,920
If not is waiting, then we want to call this set timeout.

41
00:02:35,130 --> 00:02:39,330
In other case, we won't call anything in this code will be just ignored.

42
00:02:39,690 --> 00:02:42,690
Also, it is important that we call this function directly.

43
00:02:42,900 --> 00:02:45,240
This is why I want to move it inside out.

44
00:02:45,240 --> 00:02:50,910
If condition and inside that timeout, we simply change our is rate in property to false back.

45
00:02:51,180 --> 00:02:57,120
So as you can see, this line means that at the beginning on the first call, we will call this function.

46
00:02:57,300 --> 00:02:59,940
But after this, we want to set our flag to true.

47
00:03:00,180 --> 00:03:03,150
This is why here were writing is Vaden equals true.

48
00:03:03,510 --> 00:03:09,120
Then we have a set time, for example, two seconds and after two seconds where Satan is waiting to

49
00:03:09,120 --> 00:03:14,850
false, which actually means we can call this function on the after two seconds and not before.

50
00:03:14,940 --> 00:03:18,030
So as you can see, the code is 99 percent the same.

51
00:03:18,270 --> 00:03:23,840
The main idea is that we're building here is wait and flag, and we just allow to accept this function

52
00:03:23,860 --> 00:03:25,320
once in our timeout.

53
00:03:25,350 --> 00:03:26,820
Now let's check if it's working.

54
00:03:26,850 --> 00:03:28,050
I'm reloading the page.

55
00:03:28,050 --> 00:03:34,890
We're getting directly saving input for first time and then we see onlyone save input for second time.

56
00:03:35,040 --> 00:03:37,440
And this is exactly this code process change.

57
00:03:37,590 --> 00:03:44,100
And this last timeout was two seconds and 400 milliseconds because actually here on our first try,

58
00:03:44,100 --> 00:03:46,470
we're going to throttle is waiting is false.

59
00:03:46,470 --> 00:03:48,520
We're going here, we're checking the key.

60
00:03:48,540 --> 00:03:49,440
It is not true.

61
00:03:49,610 --> 00:03:53,580
Then we're calling directly this function and we're setting our flag to true.

62
00:03:53,790 --> 00:03:59,430
And then after our set timeout and then we're changing this weight and back to false and we don't need

63
00:03:59,430 --> 00:04:03,390
here any time already because we never abort our set timeout.

64
00:04:03,510 --> 00:04:08,850
And the next question that you might get, OK, but what is the difference between the bones and throttle?

65
00:04:09,030 --> 00:04:14,760
And there's the other two said, were Colin de Bond's after use a stop to do something and work on and

66
00:04:14,760 --> 00:04:16,590
throttle for us to follow the beginning.

67
00:04:16,710 --> 00:04:19,200
And then after a certain interval of time.
