02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Adding and removing callbacks

In a previous course we talked about removing events. It's so important that we think it deserves a dedicated lesson in this course as well. You can add or remove a call back at any point by replacing the call back with nothing (Note: in programming it is called null). In this lesson we will use the eventCallback method to add and remove callbacks.

Video Transcript

00:00Like a lot of other things in the this tween library a lot of things you could do in different ways
00:06And we're just going to do adding events or callbacks in a different way using the event callback method
00:12Which exists by the way throughout the whole application from tween light all the way to timeline max now
00:19Do you have to work with it absolutely know if you're comfortable the way you are right now
00:23You don't even need to look at it, but if you want to work with a method
00:26This is how you're gonna do it. Let's see this terrain. I wanted to talk a little bit more about callbacks
00:32So in our scenario right here, we're really happy with what's going on
00:37But we only want the on complete to happen once and the reason is is because inside of our on complete
00:44We're actually doing a few things that we only want to happen once if I find our on complete right here
00:51On on him done and I'm done. We're adding a mouse event here, which we don't really want to do twice
00:57We're pausing our video. Maybe we don't want to do it twice. Maybe we only want it to happen once
01:03Now we've talked about previously that in a previous lecture in a previous course
01:08But how could you get rid of an event by basically saying you could approach the actual this and if I just
01:15Were just to hear a console that log again for a second
01:19and I just go ahead here and go to my this and
01:22I go to the vars, which are the variables that were sent initially and I just ask for the on complete I
01:32Asked for the on complete which is the variable that I sent right here in the on complete
01:36I'll see that I'll have access to it
01:38So if I just save this go to my browser click on refresh where and when our animation will complete
01:43We'll be able to see that function coming back inside of our console
01:48More than that. We said oh so that we could also delete it
01:51So we said that we could delete it and then let's see how we could delete it to make sure that it doesn't call again
01:56Because if we go ahead here and just push back a bit and press on play again
01:59We're gonna get that same function call again
02:02If we didn't want that to happen, I could literally just go to that spot in our on complete
02:08On and I'm done and instead of the instead of tracing it out. I could say hey your equal null
02:14Which basically says you are empty?
02:16And if I save this I'm basically overriding that on complete function
02:21So if we just let that animation go through first time around we should get that value
02:27But we're not oh, you know, I should I should have left that trace in so we could actually see it
02:31So let me just put here a console again
02:34console log and let me just
02:40Send in the same function here. Just really really quickly if I save this I go back to my application and click on refresh
02:46We're gonna see that first time around we're gonna see that log
02:50But second time around completely different story. So first time around is fine
02:55But if I go a little bit back press on play again, we're not gonna get that event anymore
02:59We're not gonna get that callback anymore because we overrode it. We deleted it
03:04We said we don't want that we said oh, so we talked about this in a previous course
03:07And we also said that we could delete the callback and this is the same exact rules
03:12Basically almost one by one everything we did to tween light is also relevant to timeline to tween max the timeline light to timeline max
03:20Well, we could have deleted it
03:21Oh, so I'm not gonna run it again just to show you that so we could have done that in one of these two ways or a
03:26Third one and I want to show you the third way
03:29Which is if you didn't set in your timeline directly into the parameters of the starting parameters
03:34For example, instead of setting our start function
03:37What if we only set our we didn't set our start function right here
03:41But after we created our timeline, how do we add that callback and it's really simple
03:46We just call the TL and there's a function that's called event
03:50Call back and the event callback accepts four parameters the first two are mandatory
03:56Although not really the first item is really only one that's mandatory if I call the on start
04:01Which is what I want to call and I just call it this way. I'll get back whatever the on start is
04:07In our case, I want to set it
04:09So I'm going to send also a second parameter, which is actually the function we want to call right so far
04:14So good third parameter, which is not mandatory anymore is the parameters
04:19Which is the exact same values as we would send in the on start params
04:23So if this construct is easier for you or visually more comfortable, you could literally do it in fourth parameter
04:29It would be the scope in our case
04:31We could send the exact same scope to create the exact same type of callback
04:36So if we run this just one more time
04:37we're gonna see that it's gonna do the exact same thing as we had before and we're gonna get the same values that we
04:43Had there in our traces for our on timeline start now before I wrap this up
04:48I just want to say one more thing and this is why I brought this up even though it's not saying that's completely new to
04:53Us is because very soon in a very near chapter
04:57We're gonna start talking about events and when we talk about events
05:01They're gonna be a little bit different than everything we've seen so far and events only exist in
05:07tween max and timeline max now the difference between events and and
05:14Callbacks although this name might be a little bit tricky because it says both event and callbacks
05:19But it's two different animals a callback. You could only have one of them basically
05:23We're sending in something and then it's going to be called we're sending in a function and it will be called whenever it's time for it
05:31Contrary to that an event I could send as many of the functions as I want if I wanted a few
05:36Functions to be called whenever the start happens in our case
05:40We're using callbacks so that's not possible if I call this again, and I override this with something else
05:46That's exactly what I'm doing. There's only one on star. There's only one function that could be associated with on star
05:52Now to wrap this up
05:54I just want to so to wrap this up and really make this really clear as if I go call the on start
05:59But this time around I call it inside of our
06:03La la la where are we and inside of here instead of the delete which will basically do the exact same thing
06:07We could literally just send the value of null as the function and that would kill delete that on start function once it will be
06:15Called but really in our case
06:17That probably wouldn't be the spot for that we probably would if we were doing the on start
06:20We probably want to put it here making sure that we could only start once which will probably true
06:26We could only start once which is both poetically and very accurately true
06:31All right, so that's it about this topic
06:33I'm not gonna dig much more deeper into it for now and when we go back and talk about events in a later chapter
06:40We're gonna talk about it briefly and we're gonna see how could we leverage
06:43Events to trigger more than one call back at a time if we wanted on start to call
06:47Three or four different functions, and you might ask yourself
06:50So why would you ever want to do that?
06:52That usually happens when you don't have control over those functions or you know that you have two
06:57Functions that you want to be called and you don't want to start now creating another function just for it
07:01Or it might be saying that sometimes this function is called sometimes that function is called and sometimes both are at the same time
07:08But you don't want to put mix their functionalities together
07:11Which is something that is very common in object oriented programming in general
07:15So that's it about that and as promised we're gonna go back to the add function and it see a little bit more
07:21Functionality in it in the next lecture

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!