Skip to main content

Build an analog Clock with JavaScript







Many of beginners are confused of how to build a working analog clock?. I believe that it's very simple if you are familiar with HTML CSS and JavaScript. You can make the shape of clock with the help of HTML and CSS but for making it functional, you may need to write the required script in JavaScript. If you want to know the required logic, you can watch this YouTube clip. In the end, you just need some practice over what you learnt.

The logic required for this is:

<div class="clock"> 
//defines a container element for the clock.

<div id="clock-elements"> 
//defines a container element for the clock hands and other clock elements.

<div class="stick-primary"></div> 
//defines a clock hand.

<div class="sticks stick-1"></div> 
//defines another clock hand.

<div class="sticks stick-2"></div> 
//defines another clock hand.

<div class="sticks stick-3"></div> 
//defines another clock hand.

<div class="sticks stick-4"></div> 
//defines another clock hand.

<div id="inner-circle"></div> 
//defines a circle element that is positioned at the center of the clock.

<div class="CC"></div> 
//defines a container element for the clock date.

<div class="sec-hand"></div> 
//defines an element for the second hand of the clock.

<div class="min-hand"></div> 
//defines an element for the minute hand of the clock.

<div class="hour-hand"></div> 
//defines an element for the hour hand of the clock.

<div class="date"></div> 
//defines an element to display the current date of the clock.


Now, inside script

const secHand = document.querySelector('.sec-hand'); 
//selects the HTML element with the class "sec-hand" and assigns it to a JavaScript variable called secHand.

const minHand = document.querySelector('.min-hand'); 
//selects the HTML element with the class "min-hand" and assigns it to a JavaScript variable called minHand.

const hourHand = document.querySelector('.hour-hand'); 
//selects the HTML element with the class "hour-hand" and assigns it to a JavaScript variable called hourHand.

const clockDate = document.querySelector(".date"); 
//selects the HTML element with the class "date" and assigns it to a JavaScript variable called clockDate.

function setDate() {...} 
//defines a function that sets the positions of the clock hands and updates the date.

let instance = new Date(); 
//creates a new instance of the Date() object.

let sec = instance.getSeconds(); 
//gets the current second of the minute.

let min = instance.getMinutes(); 
//gets the current minute of the hour.

let hrs = instance.getHours(); 
//gets the current hour of the day.

let day = instance.getDay(); 
//gets the current day of the week.

let secAngle = (sec / 60) * 360; 
//calculates the angle of the second hand.

secHand.style.transform = rotate(${secAngle}deg)`;
//will rotate function of clock

Close script.

I hope you may understand the concept of how to build a clock. If still you need extra help, then kindly watch this tutorial.





 

Comments

Popular posts from this blog

How to connect 000webhost to a Hostinger domain

  Today, in this tutorial, we will learn about, how to connect a free web hosting website with a paid domain. Basically, as I have hosted my website free, on 000webhost, and I registered a domain on hostinger. So, all you need is: for web hosting: 000webhost for domain: Hostinger Step-1: Kindly go to 000webhost, click on manage website. Step-2: Click on "tool" option, in the left side. Step-3: Click on option "set web address." If you want to buy a domain, you can go with first option. But here, I have already bought a domain from Hostinger. STEP-4: Click on connect a domain I already own. STEP-5:   Click on park domain. STEP-6:   Kindly write here the domain name and save it. Your domain will be parked. STEP-7: Copy these name servers of 000webhost and will paste into Hostinger's DNS. NAME SERVER to be copied :  ns01.000webhost.com ns02.000webhost.com Step-8: Go to domain setting webpage on Hostinger dashboard.  Step-8: Click on manage. Step-9: Now r...

How to get free SSL for your website?

  If you are looking for free SSL, or want to make your website secure by changing protocol from http to https, then this tutorial will help you a lot. This tutorial shows how to get a free SSL for a website hosted on 000webhost using  Cloudflare . Cloudflare offers a free SSL certificate  that will secure your website for lifetime. It’s easy to setup and you’ll get HTTP to HTTPS redirects, increased site speed, and better security. Basically, You have to register on  cloudflare  while choosing a free plan there. The next step is to add your website domain on cloudflare by updating DNS. Once you connect your website domain with cloudflare, then next step is to activate the Automatic HTTPS rewrite option, which encrypts website traffic using the HTTPS protocol. After updating the above setting, your website traffic is now encrypted and secure. I hope you have understood the concept to get free SSL for your website. If you still want to know this in detail. Please...