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

Creating a timer

Throughout this chapter we haven't done any animations. We were too busy exploring objects and the Date object specifically. In this last lesson of this chapter we're going back into animation as we create a timer. We will be using the functions setInterval and with it, we will update our screen once a second. By the end of this lesson, you will know how to create content that updates on a timely base. The main functions we are going to explore are: setInterval and clearInterval.

Video Transcript

00:00That's it. We're reaching the end of chapter four and in this chapter. We learned how to work with functions
00:07We learned how to use functions that were already there. We learned also how to instantiate objects that weren't there yet
00:15Before we wrap up
00:16I promised you that I'll show you how to update and we're gonna look at how to create an animation that updates every second because in
00:22Our case we have a counter of seconds and we really want those seconds to update once a second
00:27So let's jump right in and see how we do that
00:29Even better and to make it even better. We're gonna be using the set interval function
00:34so what I'm gonna do next is I'm gonna comment out everything really more or less because what I want to do next besides the
00:41Second so everything until here is good. And what I'm gonna do next is I'm just gonna comment everything here out
00:48All the way until the bottom here because I want to start a start a new start fresh
00:54now
00:55What I really want to do is I want to create a function here. That will be my update function
00:59I'm just gonna call it update screen and
01:02or update date how about that update date and
01:06What I want to do next is is I want to make sure and by the way you were thinking of this
01:11You know just from looking at what happened earlier even before we do this make comment down for a second
01:16We do have a slight problem because when we created our day, we didn't actually account for the actual hour
01:23So a few of our seconds were actually off because we were creating a date that was actually midnight on that date
01:29But we want we really want to do probably is put inside the actual hour and of my birth
01:35But we're gonna assume that I was born at midnight just for the case of our
01:39Course, so I'm gonna continue here and I'm gonna update my date and in my update date
01:44What I'm what I really want to do is I want to do all the stuff that we've done here more or less
01:48I'm gonna go ahead and just copy this all all of this and
01:51Put this right in here instead of having it where it was
01:55so literally what I'm doing is I'm just basically moving all this logic into a separate variable and
02:02The reason why I'm doing that is because I want to be able to call it more than once
02:06So if I call this update date and I just run it again, everything is gonna look similar
02:11But the power that we have now, oh and we have a little error here. So I'm gonna
02:15Go ahead and fix it. So it's telling me that seconds is not defined and that's absolutely true
02:20So if I come back here into my element, we'll see that my seconds variable doesn't exist
02:25So I'm gonna go ahead and turn it into a global variable
02:28Because I really want to refer to it in other functions without passing it through
02:32so I'm gonna go ahead there and get my seconds and
02:36or actually
02:39Actually actually actually we don't even need that because what we could do and what we probably want to do is we want to
02:44Reset this get seconds and do it actually and I'm gonna just copy this and move the commenting out a little bit further
02:52and
02:53what I really want to do is I want to set this up here as the first thing inside of this function because I want to
02:57Call it and each time get the updated amount of seconds that have passed
03:02so if I save this and I go back into my browser then everything should now render the way it should render and
03:08The only thing that's left for me to do is to make sure that we call this function once a second
03:13So to make that happen, I'm gonna go ahead right underneath that update date and I'm gonna call the function called set interval
03:21Now set interval accepts two parameters one is what do we want to call which is the update date and
03:29The second parameter is how every how much milliseconds in this case once a second we want to call it
03:35So once I do that and I just click on refresh on my browser
03:38You're gonna see that now we have seconds are
03:41updating and also the minutes will update once the amount of minutes have passed as well and
03:45Literally, that's all we have to do to kick up and have an animation running and living
03:51Now before I wrap this topic up
03:53I want to also just explain one more thing if we're already talking about set interval is
03:58Whenever we want to stop a set interval this function basically says hey call this function once every x amount of milliseconds
04:05This case I decided every thousand milliseconds if I ever want to stop this interval from one running
04:11I have to get the ID of the animation that started the set interval will automatically create basically a variable here
04:19So I'm gonna call it. Let's say n
04:23Interval and
04:24if I just output this this variable if I go to my console and I output into my log the n interval and
04:32I just run this you'll see that in my output. We're gonna see that when I create that animation a
04:38Number is assigned to that animation for me to be able to stop that animation again
04:43I need to be able to refer back to this variable because to stop the animation from happening. I have to call the clear
04:51Interval and I have to send into it that ID of n interval now
04:56Now if you try to stop this n interval
05:14But you don't have this variable reference if it's because you set it in a function
05:18And then it doesn't exist anymore once you're in a different function
05:21Then you're gonna be stuck and it's not gonna actually do anything for you
05:25so I'm gonna save away and I'm gonna click on refresh and
05:29If I click on refresh you'll see because I clear my interval as soon as I started our timer is not going to work
05:35anymore
05:36Now in the next lesson, we're gonna be continuing what we're doing
05:41But I'm gonna clear everything out because I want to talk about something that we haven't talked yet about and that is actually
05:48Creating our own objects. So in the next lesson
05:51What I'm gonna do is I'm gonna split that into chapter 5 already because I really want to clear everything out here and start
05:57from scratch so chapter 4 was about using objects and
06:02Chapter 5 which were just about to start is going to be about how to actually
06:07Create your own original objects. So we'll see you in the next lesson next chapter when we start working on that

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!