- [Instructor] So let's go ahead and improve
this application by communicating the waiting
time as a percentage.
So what I'm going to do is close out our terminal
and I'm going to come back up to the increment
time function, and I'm going to calculate a percentage.
So I'll create a variable that we'll call P.
So we're going to use the math dot floor method
available to us in JavaScript and we will take
the current time and divide it by the wait time.
And the reason we're using math dot floor
is because this number will give us a decimal
and what we want to do is have a percentage.
So we're going to multiply the current time
divided by the wait time times 100 and then trim
off the decimal using math dot floor.
So now we're actually going to take a little bit
more control over the terminal using process
standard output.
Process standard output std out lets us clear
the last line.
So this will get rid of the last line
that we wrote to the terminal.
And we can also move the cursor back to the start position.
Process std out dot cursor to will move
the cursor to any position that we want
so when we put in zero it will move the cursor
back to the start of the line.
So now we'll go ahead and write our message using
process std out dot write waiting.
And we'll go ahead and use our P and put
a little percent character right after that.
Great.
So the next thing we're going to do just to make
this a little bit more fun is instead
of waiting for three seconds we'll wait for five seconds.
So I'm going to go ahead and save our application,
and open up the terminal, clear the previous
output and node our timers application.
So we have a five-second delay and notice
that we're logging the time that we're waiting
as a percentage.
So the last thing I'm going to do is just clean up
the way this application reports so we don't
have the done jammed against 90%.
I can come into the timer finish just before
we write done, and I'm going to go ahead and copy
the process std out clear line and process
std out cursor to zero.
So this will clear the last percentage,
move the cursor to zero, and we'll see the done
right in the terminal on the same line
where we were reporting the percentage.
So let's go ahead and try this one more time.
Open up the terminal, node timers, and we can see
our time being communicated as a percentage.
And when we're done we will see the done message
and the program will end.
Let's just take one quick look at this to recap
what we've done.
So we have the interval that we've used
using set interval on line 22.
The interval is set to call the increment time
function that we declared on line five every half-second.
So every half-second we're going to add
to the current time and then calculate
our percentage using the current time versus
the wait time, which is the total time.
Using process standard output we're able to clear
the line, move the cursor back to zero,
and then overwrite our last percentage
with the new value.
Once the timer has finished on line 15
we declare the timer finished function
that clears the interval and then removes
the last percentage line so that we can write
done in its place.