- [Instructor] So let's include another type
of timer in this same file.
The way I'll do that is
by creating a new value called interval.
Here we're going to define a certain interval
where we want to call a function.
So it's going to be every half a second,
and we'll also set a current time of zero.
From here, we're going to create
another function called increment time,
and increment time is going to be responsible,
every 500 milliseconds, for adding
a new 500 milliseconds to the current time.
So this will continue to accumulate half-a-seconds,
kind of like a really fast clock,
a clock that ticks every half a second.
So we'll say, waitInterval, then we will console-log,
"waiting," currentTime,
divided by 1,000 seconds.
Excellent, so from here, instead of using setTimeout,
which is going to time out and call a function
after a certain time, we're going to say setInterval.
We'll pass in increment time and our waitInterval.
So I kind of think about this like a ticking clock.
Every time this unit of time elapses,
we're going to call the function.
So check this out.
Let's run it again, node global,
setting a three-second delay waiting, waiting
and notice that it calls done every time
we're done with those three seconds.
So the interval is fired every half a second,
and we see the done message being logged
because the interval is still running.
So we need to hit stop with Control + C.
That's a whole lot of ticking going on,
and we need to modify our code to clear the interval.
So we actually need to make some adjustments
to our timerFinished function.
Instead of just console-logging done,
we're going to wrap our console log
in a couple of curly braces, and before we do this,
we're going to say clearInterval and pass in our interval.
So this is going to be what we're looking for to clear.
So which is the interval that we want to clear.
So have we created this value yet? We have not.
Let's go ahead and do that,
and the place we'll do it is here line 17.
So the interval is going to be set
to this setInterval function, and now we should be able
to kind of combine the two of these
to stop the timer when we should.
So let's check it out.
We're going to run node global again,
setting a three-second delay.
We'll count up to three, and then we're done.
So we can see the time when the setTimeout is invoked,
our interval is cleared and our program exits.