Javascript clock + css
I am doing a Javascript30.com course and we have to make JS hours with seconds, minutes and hours. This is the code:
<div class="clock">
<div class="clock-face">
<div class="hand hour-hand"></div>
<div class="hand min-hand"></div>
<div class="hand second-hand"></div>
</div>
</div>
And JS:
const secondHand = document.querySelector('.second-hand');
const minsHand = document.querySelector('.min-hand');
const hourHand = document.querySelector('.hour-hand');
function setDate() {
const now = new Date();
const seconds = now.getSeconds();
const secondsDegrees = ((seconds / 60) * 360) + 90;
secondHand.style.transform = `rotate(${secondsDegrees}deg)`;
const mins = now.getMinutes();
const minsDegrees = ((mins / 60) * 360) + ((seconds/60)*6) + 90;
minsHand.style.transform = `rotate(${minsDegrees}deg)`;
const hour = now.getHours();
const hourDegrees = ((hour / 12) * 360) + ((mins/60)*30) + 90;
hourHand.style.transform = `rotate(${hourDegrees}deg)`;
}
setInterval(setDate, 1000);
setDate();
The part + 90
in the function setDate
is the offset - because we are creating the clock in JS, we turned the arrows at 90 degrees with CSS, so this is just a offset fix.
I understand everything except the statements assigned by hourDegrees
and minsDegrees
.
Why does the instructor add + ((seconds/60)*6)
both + ((mins/60)*30)
to hourDegrees
and minsDegrees
?
source to share
Every 60 seconds prepares the minute hand for its next position, and every 60 minutes the tick does the same for the hour hand.
Suppose the time 17:17:41
Calculate how many degrees the minute hand is done right now minsDegrees = (17/60) * 360 = 102
A plus;
Calculate how many degrees the past seconds have made with our minute hand,
theDegreeFromSeconds = (41/60) *
6
= 4.1
minDegree = 102 + 4.1 = 106.1
We multiply by 6 so that every second step that passes is 6 ° on the clock. This is the same for calculating the power of the hour.
source to share