Coder Social home page Coder Social logo

js-clock-intervals's Introduction

JavaScript Clock Intervals

Create a webpage that displays a clock where the second, minute, and hour hands tick.

Write CSS to have the clock face, second, minute, hour hands all display correctly on top of each other. Make sure the red second hand displays on top of the minute and hour hand. (Try changing the order the div elements are written in the HTML)

Create intervals in the JavaScript file to make the clocks tick.

Use CSS transform rotation to rotate the hands of the clock.

#myElement {
  transform: rotate(45deg);
}

Change CSS values in JavaScript by getting reference to them through the DOM and setting a new value to their CSS properties:

var degrees = 45;

var el = document.getElementById("myElement");
el.style.transform = "rotate(" + degrees + "deg)";

It would be useful to define functions that convert seconds, minutes and hours to how many degrees the hand should be rotated around the clock!

secondRotation(0); // returns 0
secondRotation(59); // returns (59 / 60) * 360

hourRotation(12); // returns 0
hourRotation(5); // returns (5 / 12) * 360

HINT!

Make sure to use the setInterval function to make a ticking function!

setInterval(function(){
    console.log("TICK!");
 }, 1000);

The first argument is a callback function - it gets triggered every 1000 milliseconds (1 second), which is the second argument.

setInterval(functionYouWantToRun, millisecondsDelay);

Bonus: Show Actual Time

Use JavaScripts Date objects to display the actual time.

var now = new Date();

Get now by declaring a new Date object. You can google for "MDN date methods" to see what helpful methods are attached to Date objects. There's helpful things like .getMonth(), .getSeconds() and who knows what else!

See the full list of methods in the left sidebar on this site: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Licensing

All content is licensed under a CC­BY­NC­SA 4.0 license. All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].

js-clock-intervals's People

Contributors

geluso avatar brandiw avatar papadavis47 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.