- Implement a Node CLI timer app which works from CLI (take time and counts down like a kitchen timer)
- Implement taking the CLI arguments: number of seconds or minutes
- Implement the asynchronous sleep function so that the app knows when the time is up
- Implement the current countdown so that the users can see how much time is left
Imagine you've been hired to implement a timer app. The app must run on the super secret NASA server so it must be a command-line program. It must be flexible enough to accept different time intervals. This app will be used by astronauts to cook food on a clandestine black ops mission.
In this project, we'll be using process
and timer functions to build a command-line interface application which takes the number of second or minutes and counts them down.
By now you should know how to access CLI arguments and use setTimeout()
. Let's get to building the Timer app!
- Create a CLI app which takes number of seconds in the following format:
ns
for n seconds, e.g., and5s
for five seconds. The command for 5 seconds must look likenode timer 5s
. - Implement the timer in
timer.js
by printing the number of seconds left on a new line. For example, if you passed10s
as your argument, you should see the text"Left: 10s"
,"Left: 9s"
, etc. printed to the console. - Use global objects
global.setInterval()
andglobal.clearInterval()
- Enhance the app by adding minutes in the following format nmin, e.g.,
1min
for 1 minute. The command for 1 minute must look likenode timer 1min
. - Utilize
process.exit()
to exit when there's an error and when there's no error but the time is up. - Use
process
to access arguments from the command line. - Test Timer with
npm test
You might want to consider using setInterval()
and clearInterval()
from Node Timers API which is identical to browser timer API. Most of front-end developers are already familiar with setInterval()
so we won't duplicate and explain it here. But here are the links if you need a refresher:
View node-timer-lab on Learn.co and start learning to code for free.
View Node CLI Timer App on Learn.co and start learning to code for free.