-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (93 loc) · 2.49 KB
/
index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// DOM Elements
const time = document.getElementById('time');
const greeting = document.getElementById('greeting');
const name = document.getElementById('name');
const focus = document.getElementById('focus');
// Options
const showAmPm = true;
// Show Time
function showTime() {
let today = new Date(),
hour = today.getHours(),
min = today.getMinutes(),
sec = today.getSeconds();
// Set AM or PM
const amPm = hour >= 12 ? 'PM' : 'AM';
// 12hr Format
hour = hour % 12 || 12;
// OutPut Time
time.innerHTML = `${hour}<span>:</span>${addZero(min)}<span>:</span>${addZero(sec)} ${showAmPm ? amPm : ''}`;
setTimeout(showTime, 1000);
}
// Add Zeros
function addZero(n) {
return (parseInt(n, 10) < 10 ? '0' : '') + n;
}
//Set Background & greeting
function setBgGreet() {
let today = new Date(),
hour = today.getHours();
if (hour < 12) {
// Morning
document.body.style.backgroundImage = "url('img/morning.jpg')";
greeting.textContent = 'Good Morning';
} else if (hour < 18) {
// Afternoon
document.body.style.backgroundImage = "url('img/afternoon.jpg')";
greeting.textContent = 'Good Afternoon';
} else {
// Evening
document.body.style.backgroundImage = "url('img/night.jpg')";
greeting.textContent = 'Good Evening';
document.body.style.color = 'white';
}
}
// Get Name
function getName() {
if (localStorage.getItem('name') === null) {
name.textContent = '[Enter Name]';
} else {
name.textContent = localStorage.getItem('name');
}
}
// Set Name
function setName(e) {
if(e.type === 'keypress') {
// Make sure enter is pressed
if (e.which == 13 || e.keyCode == 13) {
localStorage.setItem('name', e.target.innerText);
name.blur();
}
} else {
localStorage.setItem('name', e.target.innerText);
}
}
// Set Focus
function setFocus(e) {
if(e.type === 'keypress') {
// Make sure enter is pressed
if (e.which == 13 || e.keyCode == 13) {
localStorage.setItem('focus', e.target.innerText);
name.blur();
}
} else {
localStorage.setItem('focus', e.target.innerText);
}
}
// Get Focus
function getFocus() {
if (localStorage.getItem('focus') === null) {
focus.textContent = '[Enter Focus]';
} else {
focus.textContent = localStorage.getItem('focus');
}
}
name.addEventListener('keypress', setName);
name.addEventListener('blur', setName);
focus.addEventListener('keypress', setFocus);
focus.addEventListener('blur', setFocus);
// Run
showTime();
setBgGreet();
getName();
getFocus();