a.We ever change we can only we can change it in one place here. All right so let's add a couple of listeners who are text area so text area events listener key down in Key down we are going search digital marketing companies in madhapur to trigger you know it will trigger a function called add text and we'll also do text area event listener here.
b. This one will be removed text. Let's go ahead and to find those functions function will take in the event just in case and this one we can do. PARAH inner text is just equal to parrot text right OK. And then finally we can do Konst part TX equals user is typing just that we can define it as a constant. If That's where you find digital marketing company bangalore define users typing. OK, that's all that function is going to do. Next function remove text and Peridot inner text equals an empty string.
c. This should sort of work. Let's see what happens. Refresh it. And it is going to type and getting rid of the Consul here I'm going to say hello how are you doing. And that is pretty wonky right. Basically, any time we let up on a key just immediately fire is the key event which is not really a good user experience. Want it to kind of wait a second before it goes for digital marketing companies in madhapur does the key of it. So here it's just kind of going nuts. No one can type fast enough even if I search as fast as I can.
Introduction to key events
a. It still just fires the key up. As soon as I let go of the key right if I hold down a key it doesn't do it. Here I'm holding down a key. But as soon as I get up it goes away which is not really ideal. So how can we fix that? What's the solution. Well, what we can do is we can do what's called the bouncing time. In other words, you can do this if you ever want your event to not fire.
b. You know too many times in our case our event just fires immediately after key up all different kinds of events that you can listen for. And if you have something complicated that runs every time, for example, a key up event happens it maybe has to run a whole bunch of functions or something that might slow down your app so you might say well I only want this event to digital marketing companies in madhapur fire after a certain amount of time and only once per second for example.
a. So how can we do that? Well, we can use a timeout for that. And essentially we don't really need a timeout on the text unless we want one. But for the removed text we don't want this to fire immediately after we do a key up. We can say OK wait for a second after we let up the key. So it's going to look something like that. So, first of all, I will just initialize an undefined variable or a letter here. So let timer so this is undefined and I'm doing this because I'm going to be using it later. And then in here, we're going to basically do a set time an I know we're going to Taimur equal set time. This on a separate line set time and set time.
b. It takes a function I'm doing it the E- way. Takes a function and it takes the time to wait until it invokes that function essentially. And then here we can move this paradigm in a text and move it right here. And this will not happen until after a second passes by now with our current implementation. Let's see digital marketing companies in madhapur what happens now. Not quite done yet but let's see. Someone to say hello. How are you? It's a still kind of weird. It still waits a second. But how are you doing?
a. It still fires every time. So how can we avoid that? What if it detects the user is still typing. You know if the user doesn't type anything for a second. We don't want it to fire you know retroactively for every time a key up was triggered because that's what it's doing right now. Soh d l lo. It's kind of tedious. So what we can do instead is every time it detects a keep event it's going to run remove text and instead of just doing a new timer every time or reassigning it to a new timer every time we just clear timeout and clear time.
b. If you're not aware we'll clear any timer. If you give it something it doesn't understand or undefined it's not going to break it's not going to do anything bad. But if it does have a timer it clears that timers so it stops whatever was going to happen inside the set time. So just to walk through what will happen. OK so we start to type key down events are firing the lead up the key. Soon as we let up the key it runs dysfunction. And it says OK I better clear this time. Oh, there's no time.
Great. I'm not going to worry about that line. Otherwise, it's going to do timer equals set time blah blah blah and then it's going to wait a second and then it's going to run the digital marketing companies in madhapur function inside the set time. However in between that if we start to type again soon as we get up remove text it's going to say a clear time of oh I have a timeout here waiting to clear that and then it just sets a new time. OK. So let's see that in action refresh your and we will say hello how are you. And then it goes away. And so still have this text in here which should not be there.
a. Is it just empty? Initially, it should be. Try it again. OK so now there's nothing there. H t oo. Are you OK? Wait a second. And then it goes away. So it only really fired the one time because I mean it was firing the event every time and it was firing the function every time. It just we was clearing our time out if digital marketing companies in madhapur there did happen to be a time out. And if you want proof of that you can do something like let's count zero. Every time we run this function we can do count plus equals one and we can a log to count. Let's take a look at our console refresh H. D. Or you so it is firing.
b. It's just we're clearing our time. And again this is not required by and any means but if you are encountering no weirdness with your event saying hey this is firing way too often way more than it should consider using time out to debunk the time. This is kind of a common design pattern and you could shorten this you know whatever you put in here it would be in milliseconds. You can make it longer. Whatever works for you.