setInterval
Introducing time into our coding. this video continues the last course where we left of building out our basic class structure. in this video we tackle setInterval
Video Transcript
00:00The next step that we want to do is, now that we have our class, let's start working in this class and building out the logic.
00:06What we want to actually do is, as soon as we get a valid time, to actually start counting, okay?
00:13To do that, what I want to do is, at first, instead of setting my seconds right here, I'm just going to cut that,
00:19and what I want to do is, at first, I'm going to send it over to a function which is going to start our application,
00:25which is going to be a public function as well.
00:27So you know what, let's hang on there for one second, and we're just going to create a new public function,
00:32and we're going to call it start timer.
00:35And start timer is going to accept seconds as well, which will be an integer, but this time around mandatory valid.
00:47It's not going to return anything, but it's a mandatory value.
00:54So the next step now that we have our start timer is, instead of calling our total seconds,
00:58instead of setting our total seconds immediately, what I want to do is to ask if our seconds exist,
01:03which means if the value is not zero, right?
01:06Because if the value would be zero, our if statement will come back as false, so it would never call.
01:11So if our if seconds begins, then we want to call our start timer, and we want to send to it the amount of seconds, okay?
01:20Now, same thing down here, so we want to set that value up, okay?
01:26Now, after we set that value up, there's a few things that we want to do.
01:29One is we're going to start using what's something that's called an interval,
01:32and we're going to revisit this as newer versions of Flash in ActionScript 3.
01:36Adobe kind of recommends that we don't work with intervals, but intervals are so popular and so common
01:41that it's a good thing to know them and know how to use them.
01:44So we're going to start up our application by importing the interval functions.
01:49There are two functions, and we will find them, and if we click on import, we're going to type Flash,
01:55and then utils, and find our set interval, and we're going to import again Flash, utils,
02:04and then we're going to set call clear interval.
02:07So these are two functions that they're in charge of basically calling repetitively, repeatedly, without using events.
02:14So let's see how they work.
02:16First of all, to use our set interval, before we even, we're going to call it right here in our start timer.
02:21And what we want to do is we want to call our set interval every second.
02:27Now, what do we want to do every second?
02:29Let's first create that function.
02:31First of all, we want it to be a private function.
02:33We don't want anyone to have access to it.
02:35Let me just toggle my screen to be full screen.
02:39And what I want to do is I want to add another private function,
02:42and this private function will just do one thing, and it will just be update.
02:46It will update our timer.
02:48It's not going to accept anything.
02:50It's not going to return anything.
02:52It's just going to make an update.
02:54Now, a fantastic shortcut.
02:55We just want to do two things.
02:57First of all, we want to take our total seconds,
03:00and we want to subtract one second out of it.
03:03Now, after we subtract one second out of it,
03:05we want to then check to see if our total seconds has reached zero.
03:10To do that, we could use the same strategy that we've done before,
03:14is ask if total seconds,
03:18if not total seconds in this timer round,
03:21if it is, well, if the value is zero,
03:24and only if it is zero, we want to call this function.
03:27If it's not total seconds, then we want to stop our interval.
03:31We want to stop our function.
03:33We're going to look at how do we stop in just a second.
03:37All right, so we have our update,
03:39which basically takes a second off each time it's called.
03:43Now, let's call it.
03:44To make it happen, we're going to go back into our start timer.
03:48And in our start timer, we want to set an interval.
03:51Set interval.
03:54And when I'm going to open up my round bracket,
03:57you're going to see that it's expecting a function
03:59and the amount of delay, which is the amount of milliseconds
04:02that we want to wait between calls.
04:05And on top of that, it returns a Uint.
04:08Now, that Uint is an identifier
04:10that basically recognizes that interval.
04:13Now, if we don't capture that number that's coming back,
04:18we will never be able to terminate that interval.
04:20And that's why Adobe doesn't really want you to work with it,
04:23because it puts you at risk.
04:25Because if you don't stop your interval,
04:28then your interval is going to run endlessly,
04:31creating memory leaks.
04:33All right, so let's see how we could deal with that problem.
04:36First of all, we want to call our update function.
04:39And we want to call it every thousand milliseconds.
04:43So our update is going to be called every second.
04:48Now, let me just run it for a second.
04:50And this time around, instead of sending it 10,
04:52let's just send three, so we won't have to wait 10 seconds.
04:55Then we're just going to comment out our get tools.
04:57So I can just just see it in action and see where it fails us.
05:00So as our application starts running,
05:03first of all, let's see what our problem is.
05:06First of all, we're not seeing anything, right?
05:08We're not seeing our intervals.
05:09Let's see what's going on here.
05:11So if second start timer, that seems right, right?
05:16Our application, we're setting a new timeout.
05:19We're sending three to it.
05:21Three is coming in our timeout.
05:23It's asking if three, three is valid.
05:25Then it calls start timeout, gets here.
05:28Our total seconds is set to three.
05:30Our function update should be called once every thousand milliseconds.
05:35And let's just try to figure out why this is doing this.
05:38Let's just put here a little trace,
05:40just to see that we're actually getting all the way to this point,
05:42because we think that we are, right?
05:44So if we run our application one more time, okay,
05:46so we are getting to that place.
05:48But for some reason, our set interval is not actually doing
05:52what we hoped it would do.
05:54Or maybe it is, right?
05:56And it is.
05:57The reason is that we're not seeing anything else,
05:59because we're not really tracing anything out.
06:01So just to see it in action,
06:04let's just trace that after we subtract our,
06:06you know, just in the end of our function,
06:08let's just trace out here our total seconds.
06:12All right, that was a close one.
06:14For some reason, now it looks weird.
06:17All right, so two, one, zero,
06:20uh-oh, minus one, minus two, minus three.
06:23Right, because our interval is continuing on without ending.
