top of page

INTRODUCTION OF FIRING AND FUNCTION REMOVING EVENTS

1. Basic Information

You know we have a couple of events here and we give them each an anonymous function to run if it detects the click and this it makes sense but it's more common. Just to give a reference to a function that will fire if it does pick up on a click or a Focus event for example. So let's say we want to extract this code. Right now we have list of digital maketing compaines in delhi ncr which doing similar things we're doing alert. And then a couple of console logs Let's extract this into a separate function.

 

So we will do a function log event, for example, takes in the event and what we want to have happened if we want to cancel logged the event type a pretty straight forward. So instead of giving it   to digital agency function we just give a reference to log event like so you don't want to actually invoke the function. That's why I'm not doing the brackets event like this.

2. Function and Module

I just want a reference so that it knows to call this function if it does pick up a click event that will do the same. Here list of digital marketing companies in delhi ncr and log event here as well. Log event and recall that it's automatically passed an event object. And I'm just going to make sure that I have a reference to it in my console I could call this anything common to see a called E.. For example as well. So let's see if we have figured out our logic correctly here and we'll refresh. Keep an eye on our Konsole quick case we've got focus event firing and quick event firing and we've extracted the logic out into our function. Now we've got to focus our focus on a quick and focused effort.

That means we can do kind of different types of things in our function. For example, we can check that type so we can say if type equals quick console log quick if Iraq type equals focus console log focus else. Well, let's just return false. If we don't want our focus events to fire anything. Let's take a look if we get our console lock focus and click happening focus it doesn't do anything because we put that in our statement. Right.

3.Brackets and E-way

So you can imagine how this can be pretty useful. You could also kind of extracted so you kind of nested functions if you ever need to right if it if it's type quick run one kind of function. If it's type focused to another but again you don't want to just invoke the function right away because if I tried to do this let's see what happens. Refresh it is not defined so it doesn't really know what to do at this point and even if I don't give it a variable or an argument doesn't really understand. So if you're coming across this issue you don't want to actually invoke the function which is what adding the brackets does.

 

You just want to add a reference to the function and just as a quick reminder you wondering hey how come I can reference the function before I find it. Recall that when we host a function like so we can invoke it before defining the function. If you're defining it this way Konst log event equals i.e. if you doing it the E- way. Let's see if this works.

5. Log Events

 

a. Log event is not defined because it doesn't understand yet at this point with our function expression doesn't understand with log event, is it? So that's kind of a tangent but just something I wanted to point out. If we had list of digital marketing companies in delhi ncr and this defined up here instead it then it should work. At this point, we have defined the function. And so one of the foibles you might say of javascript if you're not quite used to it yet I will undo it and set it the way it was though.

b. I should also mention that like we can add event listeners we can also remove event listeners. So let's say instead of just Consta logging when we get log event we want to make our button clickable but only one time. So let's say you can click it and then check list of digital marketing companies in delhi ncr after you click it it's not going to be clickable anymore you may have seen this before online if you have like a button you click it becomes greyed out and the text changes for example so that it looks something like.

c. So what we'll do is we'll change the inner text of the button after it's detected a click or a Focus event. You know I'm going to give it to my folks out event and you're not going to get it my focus event as well, in fact, I just I'll just comment it out for reference for later. So we can do is we can say clickable button that inner text equals please no click like so. All right. And I just want to do a sanity check to make sure it's still working. Quick no quick. OK cool. And I'm also going see the list of digital marketing companies in delhi ncr and  to log the event type and Ideally, this should only ever fire once. Along with this. Let's do a sanity check again quick. OK. And then we keep clicking you can see a certain increment up five times six seven eight.

d. I don't want that and I only ever want this to fire one time. Let's see if we can figure that out. So what we do is we'll add the text clickable button dot remove event listener and we will remove the click listener and we'll just give it log event again to fire. And when Move was a console log one line below. All right. Let's see if we can't get this to run. And you know it is sort of the inner text being that does the dot type click here.

 

e. OK click and we can see the list of digital marketing companies in delhi ncr it's not firing anymore. So it doesn't get to that point at console loves it once and then it removed the event listener for us. So if instead of the type we want to make sure that it is something unique Let's figure that out. So you know what I'm going to quickly take a peek at the event the quick just needed to load. OK. So it's a mouse event. And if there is something unique I think I can do some kind of time stamp on it.

6. Conclusion

a.Timestep so this should always be unique. So you can see right now ninety-two points eighty-seven. Let me refresh it and we'll take a look at list of digital marketing companies in delhi ncr the time stamp again. So this will be a unique number let's see. Yes, it's different. So this is a good way to check that you are only doing something once. So each time stamp Let's try that here and I might need to camel case undefined. Yeah, will do. A time stamp like socage for that to refresh.

 

b. Click here. There we go. And we can click as many times as we need and we can see the time stamp is not changing at all. So far the button once we cancel logged once and then we removed the event listener right so we can click as many times we want the event listener has been removed which is the effect that I wanted.

bottom of page