MoreLesson 263Free preview
Countdown Timer
This lesson is part of How To · All levels
Summary
The countdown timer counts the difference between the present time and the target date. JavaScript provides Date to take time, then the Interval set ups the view every second. When the time runs out, pause the intervals and display a clear message.
Key points
- Date.now() or new Date().getTime() gives time in milliseconds.
- The time interval is converted to days, hours, minutes, and seconds by dividing and Math.floor.
- PadStart(2, '0') adds zero to the front of the one-digit number.
- Save the ID from the Interval set so it can be stopped with clearInterval.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.