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!
nsfor n seconds, e.g., and
5sfor five seconds. The command for 5 seconds must look like
node timer 5s.
timer.jsby printing the number of seconds left on a new line. For example, if you passed
10sas your argument, you should see the text
"Left: 9s", etc. printed to the console.
1minfor 1 minute. The command for 1 minute must look like
node timer 1min.
process.exit()to exit when there's an error and when there's no error but the time is up.
processto access arguments from the command line.
You might want to consider using
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.