-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.js
30 lines (24 loc) · 1.14 KB
/
clock.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
const audio = new Audio('./tick.mp3');
function clock (timezone) {
let now = new Date(new Date().toLocaleString("en-US", {timeZone: timezone}));
let seconds = now.getSeconds();
let minute = now.getMinutes();
let hour = now.getHours();
let ampm = (hour >= 12) ? "P.M" : "A.M";
// For real hour hand movement
hour = hour + (minute/60);
// For real minute hand movement
minute = minute + (seconds/60);
document.querySelector('.hand.second').style.transform = `rotate(${seconds * 6 + 90}deg)`;
document.querySelector('.hand.minute').style.transform = `rotate(${minute * 6 + 90}deg)`;
document.querySelector('.hand.hour').style.transform = `rotate(${hour * 30 + 90}deg)`;
document.querySelector('.ampm').textContent = ampm;
audio.currentTime = 0;
audio.play().catch(error => {});
}
var startClock = setInterval( () => {clock('Africa/Lagos')}, 1000);
document.querySelector('.timezones').addEventListener('change', (e) => {
clearInterval(startClock);
startClock = setInterval(()=>{clock(e.target.value)}, 1000);
document.querySelector('h1').textContent = document.querySelector(`option[value="${e.target.value}"]`).textContent;
});