- [Lecturer] So, this clock we created is great at showing the time the browser window was recently updated, not so great at showing the current time because it doesn't move. That's because the script we've created is static, it runs once, when the browser is loaded, and then does nothing. So, if we want the current time, we have to keep reloading the browser. Of course, you could sit here and do that every second, and the clock would move, but that's not practical by any means. We need to somehow rerun this script every second to update the position of the second, minute, and hour hands.
To do that, we'll first wrap all our script in a function. I'll call it: runTheClock. And just indent all the contents, we see the inheritance here. Then, we need to run this function every second. That's done using the setInterval method. setInterval repeatedly calls a function, or executes a code snippet, with a fixed time delay between each call. That's exactly what we want.
The time delay out of the box is in milliseconds, so we need to reduce that down to seconds 'cause we don't have to do this on a millisecond basis, that's just a waste of resources. Back in my code, I'll make some space at the bottom, then I set up a new variable, I'll call it: interval. This is just so we can refer to the setInterval later if we want a bigger script. Then, I set an interval and this interval we'll call: runTheClock every thousand milliseconds, which is one second.
So, every second, we run the full function, we get the date, we the find the hour, minute, and second, then we do the math to figure out what that equates to on the clock face, and we rotate the items to position correctly. Save that. Load it in the browser. And the clock works. Awesome. You'll also notice, we have this really nice animation thing going on with the arm here. That's powered by the transition: transform point five second ease-in-out section down here in the CSS.
This looks great, as far as I'm concerned at least, and it works. However, there's a bit of a problem, and we have to wait until the second hand goes all the way to the top to see it and when you see it, you'll see it's a real problem. Here we go, pay very close attention to the second arm right now. Oh, what happened? Instead of going from the 59th second to the top of the clock, it went from the 59th second and then spun back around to the top on the other side.
That looks pretty weird. What's happening here? Well, we're getting the second position and then we're converting it to a number of degrees on a circle that is 360 degrees. So that means, when the second goes back to zero, the position of the arm goes back to zero and we get that weird motion. Now, it's easy enough to solve, we can take the transition away, so I'll just comment it out, now the second arm will move on a tick, tick basis. This still looks great and we don't get that skipping action, but, we're lacking the finesse of that nice animation.
So, in the next movie, I'll show you how to get around this problem.
Author
Updated
4/1/2019Released
5/17/2017Through practical examples and mini-projects, this course helps you build your understanding of JavaScript piece by piece, from core principles like variables, data types, conditionals, and functions through advanced topics including loops, closures, and DOM scripting. Along the way, you will also be introduced to some ES6 and the basics of JavaScript libraries.
- What is JavaScript?
- Working with data
- Using functions and objects
- Working with JavaScript and the DOM
- Changing DOM elements
- Handling events
- Working with loops
- Making images responsive using markup
- Troubleshooting code
- Validating functionality
- Minifying JavaScript
Skill Level Beginner
Duration
Views
Related Courses
-
Introduction
-
Welcome1m 7s
-
-
1. JavaScript: An Introduction
-
What is JavaScript?2m 38s
-
-
2. The Basics
-
Introducing the browser console10m 31s
-
3. Working with data
-
Data types in JavaScript4m 2s
-
Arrays2m 20s
-
4. Functions and Objects
-
Functions in JavaScript3m 28s
-
Build a basic function3m 29s
-
Anonymous functions5m 11s
-
Variable scope3m 17s
-
ES2015: let and const6m 12s
-
Make sense of objects3m 19s
-
Object constructors6m 16s
-
Closures8m 11s
-
-
5. JavaScript and the DOM, Part 1: Changing DOM Elements
-
Access and change elements4m 33s
-
Access and change classes3m 45s
-
Access and change attributes4m 53s
-
Add DOM elements6m 56s
-
6. Project: Create an Analog Clock
-
Use CSS to move clock hands3m 49s
-
7. JavaScript and the DOM, Part 2: Events
-
What are DOM events?1m 31s
-
Some typical DOM events1m 59s
-
Add and use event listeners6m 51s
-
-
8. Project: Typing Speed Tester
-
Rundown of HTML markup2m 58s
-
Build a count-up timer5m 56s
-
Add a reset button5m 3s
-
-
9. Loops
-
Loops3m 37s
-
Looping through arrays4m 7s
-
Break and continue loops7m 9s
-
-
10. Project: Automated Responsive Images Markup
-
Project breakdown1m 55s
-
Rundown of project setup3m 26s
-
-
11. Troubleshooting, Validating, and Minifying JavaScript
-
Troubleshooting JavaScript7m 20s
-
Online script linting5m 57s
-
Automate script linting8m 24s
-
Online script minification2m 50s
-
Automate script minification2m 24s
-
12. Bonus Chapter: Ask the Instructor
-
What are arrow functions?3m 11s
-
What does the % symbol do?3m 47s
-
-
Conclusion
-
Next Steps1m 55s
-
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.
CancelTake notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.
Share this video
Embed this video
Video: Make the clock move forward second by second