-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
68 lines (48 loc) · 1.78 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const hourNumbers = document.querySelector('.hour-numbers');
const seconds = document.querySelector('.seconds-hand');
const numberElement = [];
const secondsElement = [];
for (let i = 1; i <= 12; i++) {
numberElement.push(`<span style="--index:${i}"><p>${i}</p></span>`);
}
hourNumbers.insertAdjacentHTML("afterbegin", numberElement.join(""));
for (let i = 1; i <= 60; i++) {
secondsElement.push(`<span style="--index:${i}"><p><h>${i}</h></p></span>`);
}
seconds.insertAdjacentHTML("afterbegin", secondsElement.join(""));
//time
document.getElementById('playButton').addEventListener('click', startClock);
document.getElementById('pauseButton').addEventListener('click', pauseClock);
let isClockRunning = true;
let intervalId;
function startClock() {
intervalId = setInterval(updateClock, 500);
isClockRunning = true;
}
function pauseClock() {
clearInterval(intervalId);
isClockRunning = false;
}
function toggleClock() {
if (isClockRunning) {
pauseClock();
} else {
startClock();
}
}
function updateClock() {
const now = new Date();
const hours = now.getHours();
const minutes = now.getMinutes();
const seconds = now.getSeconds();
const hourHand = document.getElementById('hour');
const minuteHand = document.getElementById('min');
const secondHand = document.getElementById('sec');
const hourDegrees = ((hours % 12) + minutes / 60) * 30;
const minuteDegrees = (minutes + seconds / 60) * 6;
const secondDegrees = seconds * 6;
hourHand.style.transform = `rotate(${hourDegrees}deg)`;
minuteHand.style.transform = `rotate(${minuteDegrees}deg)`;
secondHand.style.transform = `rotate(${secondDegrees}deg)`;
}
startClock();