The Timer Class
In this video we are switching our usage of intervals for the more modern Timer class.
Video Transcript
00:00I kind of want to take a step back.
00:02We've kind of did something that Adobe doesn't really like,
00:05which is that we're working with a set interval
00:07and a clear interval.
00:08Even though we worked really nicely
00:10and we made sure that our events are clean,
00:12what would happen in a scenario, for example,
00:15where someone deletes our two, right?
00:18They delete after the event starts.
00:21Our event will continue running even though the user deleted it.
00:24So for example, if I just put inside of here nothing,
00:27I've just emptied it.
00:28There's no access to it anymore.
00:30But still, our event will continue running,
00:33which is a problem, right?
00:35So we're going to have to figure out a way how to solve that.
00:37But with that, we want to work with events
00:39just because the model of Flash and kind of Flash
00:42is very, very heavy on events.
00:44So whenever we could use events,
00:46we'll probably use them.
00:47But a lot of times, I personally will use set intervals
00:51and clear intervals.
00:52But from my perspective is when you're saying something
00:55that you think more than one listener might listen to,
00:58then you would work with events.
01:00If there's a scenario in the future
01:01where you think more than one thing would listen to it,
01:04then you would work with events.
01:05Then you would dispatch an event, such as in a scenario
01:08where we're dispatching on event change.
01:11It doesn't really know what's listening to it.
01:13A lot of stuff could listen to it.
01:14And it kind of makes sense.
01:17With that said, what I want to do
01:18is I want the Adobe way and the right way.
01:21Basically, instead of using our intervals,
01:23I'm going to delete those intervals.
01:25And we're going to work with a different object, which
01:27is called the timer object, which if we import it,
01:30it's going to be in our flash.
01:32It's going to be inside of our time, utils, timer.
01:37And we have our timer class.
01:39So the next thing I want to do is
01:41I want to create a new timer.
01:42So first of all, I'm going to delete the interval.
01:46We're not going to compile it right now.
01:48If we would, it would throw errors
01:50because we're using that variable
01:51throughout our application.
01:53But instead of that, what I want to do
01:56is I want to create a new variable that
01:57is going to be a timer variable.
01:59So I'm just going to call it timer in lowercase.
02:02And it's going to be a type of timer, which
02:04is uppercase because it's a class.
02:07As soon as our application begins, what I want to do
02:10is I want to create a new timer.
02:12So I'm going to take my timer and say you're a new timer.
02:16Open bracket and close bracket.
02:18Now the delay that we want to give to it again
02:20is 100 milliseconds, 1,000 milliseconds.
02:24The repeat count, we want to give it zero
02:25because we want it to be never ending.
02:28If we would give it a repeat count of 10,
02:30we would only do it 10 times, which is more control.
02:34So we don't want to have an ending to it by default.
02:37Now the next thing that we want to do
02:39is we want to start adding event listeners.
02:41And we want to start our timer.
02:44To do that, first of all, what we want to do
02:46is when our start timer begins and we
02:49know that we're about to start our timer, first of all,
02:52we want to, if it's not running already,
02:53we want to start it.
02:55But if it has already started, it doesn't really matter.
02:57So we don't even care anymore if, besides caring
03:01that if we have a value there and which that we do.
03:04So if our second is there, then we want to start our timer.
03:08Starting our timer would then basically,
03:10we would call our timer, and we would ask it to start.
03:17If we get a value that's not valid,
03:19we want to stop our timer.
03:20So in an L scenario, I'm going to ask my timer to stop.
03:28So so far, so good.
03:29We're just starting our timer, stopping it.
03:32Now, we don't have any method which is called yet, right?
03:34So that we're going to have to do that in a second.
03:36But let's go down to our last piece, which
03:39is when our update is completed.
03:41And it found out that the time is up.
03:45And when the time is up, what we want to do is the same thing.
03:47We want to tell our timer to stop.
03:52Fantastic.
03:54The last thing that's left for us to do
03:55is actually call the event.
03:57Because our timer has events.
03:59So if I go back to my timer, I could go to my timer
04:02right before I'm starting my timer.
04:04And I could just tell my timer, timer.addEventListener,
04:08I could add it once and have it work throughout.
04:11And I'm going to call it on the timer event.timer.
04:15The timer event.timer is, again, it's a string
04:17that basically will be called every single time our timer is
04:20called, which is once every second.
04:22And then we want to call our event.
04:25Now, we have an event called update.
04:27But our update is not expecting an event.
04:31We could change it and have it expect an event.
04:33But how about instead, we just create a new function,
04:36a new method, and we could call it on timer.
04:40And that's all we really care about.
04:42So we send our two pieces of information
04:44what we're listening to, which is our timer.
04:47And again, it is importing by default from the flash
04:50that event's that timer event.
04:54I'm going to grab my on timer, and I'm just
04:56going to scroll all my way down
04:57because I want this to be a private function.
04:59I don't want anyone to have access to it
05:01besides this class.
05:04And function on timer, which is going
05:07to get an event, which is going to be a timer event.
05:11It's not going to return anything.
05:13And inside of my timer event, I could call my update function.
05:17So that's all I had to do to get my application to run.
05:20Now, I'm going to run it, and then we're
05:21going to talk about one more thing just
05:23to make sure that our application still works.
05:26So if we run our application, we're
05:27going to see that our application is working again.
05:31So let's also test it out and just see if it works.
05:33Also, when we hit that few seconds
05:36just before the timer is done.
05:38So if we try our 30, three seconds,
05:41we'll have three, two, one, done, all right?
05:45So everything seems to be working as it was before.
05:48Now, let's just go over this.
05:50One thing that we're doing here
05:51is we're setting out a new event.
05:52We're never removing it.
05:55So we have only one event.
05:57It makes it much easier because now we
05:58don't have to clear an interval, add an interval.
06:02We don't have to make sure that is it on, is it off?
06:04Because we have one object.
06:06We're going to have only one event.
06:08It's not that consuming because as long as it's not being
06:10called because we're starting it and stopping it,
06:13so we don't have to worry about it being called.
06:15As long as our object is still there,
06:16then it's less work for us.
