- [Instructor] So far we've created a small
application that uses the set time out function
to create a three-second delay.
We can improve this application by using
the set interval function to run an interval
that intermittently reports on how long we've waited.
So in order to get this started I'm going to come up
here to line one of our timers dot JS file
and I'm going to go ahead and add a wait interval.
The wait interval will be half a second
so 500 milliseconds.
I'm also going to create a variable for the current time.
And current time will equal zero.
Most of the variables that I declare, I declare
with const because I don't want users to be able
to change their value.
However, any time I want to change the value
of a variable I'll have to declare that variable with let.
So the other thing that we're going to do
is we are going to create another function here.
Now, I'll declare the increment time function
with an arrow function but because we have
several lines we're going to use the brackets
to enclose those lines.
So what we're going to do is take the current time
and we are going to increment it by the wait interval.
And then we are also add a log to the console
waiting current time divided by 1,000 seconds.
Great, so I can go ahead and save this.
And now the next thing I'm going to go ahead and do
is right above my set time out here I'm going
to add a set interval.
Set interval is very similar to set time out.
The first argument it takes is a function.
And the next function is the time
that the interval should wait.
So we'll use wait interval, which is a half a second.
So what happens with set interval is it will call
the increment time function over and over
and over again every half-second.
Let's take a look at this.
I'll go ahead and save this file and come out
to the terminal, and node timers, run our application.
And we can see that we started a three-second
delay but the interval is firing over and over
again every half-seconds.
So we are incrementing the waiting time.
Notice there is a done log to the console after 2.5 seconds.
So that's when the application can finish.
Because we set an interval and we never
cleared it this process will run indefinitely.
So what we have to do is clear the interval.
I'm going to hit control C to stop the process
in the terminal, clear just to set me up
for the next run, and I'll close the terminal
for a second.
And now I'm going to modify the timer finished function.
So the first thing I want to do if I need to clear
the interval, I have to have a variable for the interval.
And whenever we set an interval the set interval
function will actually return the interval itself
so that we can clear it later.
So when the timer is finished I'm actually going
to do a couple of things.
So this is no longer going to be a one-line function.
I have to use the brackets so that I can console
log that the timer has finished as well as clear
the interval.
So now we'll go out to the terminal and run this file.
And we see our three-second delay and our waiting
messages and also notice that when we are done
we clear the interval so the interval's still
not ticking, and because the interval's not
ticking the application exits.