-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
123 lines (104 loc) · 3.03 KB
/
Copy pathscript.js
File metadata and controls
123 lines (104 loc) · 3.03 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
document.addEventListener("DOMContentLoaded", function () {
// Select DOM Elements
const time = document.getElementById("time");
const greeting = document.getElementById("greeting");
const nameInput = document.getElementById("name");
const focusInput = document.getElementById("focus");
// Options
const showAmPm = true;
// Show Time
function showTime() {
const today = new Date();
let hour = today.getHours();
const min = today.getMinutes();
const sec = today.getSeconds();
// Set AM or PM
const amPm = hour >= 12 ? "PM" : "AM";
// Convert to 12-hour format
hour = hour % 12 || 12;
// Update Time Display
time.innerHTML = `${hour}<span>:</span>${addZero(
min
)}<span>:</span>${addZero(sec)} ${showAmPm ? amPm : ""}`;
// Update time every second
setTimeout(showTime, 1000);
}
// Add Leading Zero to Numbers < 10
function addZero(n) {
return (parseInt(n, 10) < 10 ? "0" : "") + n;
}
// Set Background and Greeting
function setBgGreet() {
const hour = new Date().getHours();
if (hour < 12) {
document.body.style.backgroundImage =
"url('https://i.ibb.co/7vDLJFb/morning.jpg')";
greeting.textContent = "Good Morning, ";
} else if (hour < 18) {
document.body.style.backgroundImage = "url('afternoon.jpg')";
greeting.textContent = "Good Afternoon, ";
} else {
document.body.style.backgroundImage = "url('night.jpg')";
greeting.textContent = "Good Evening, ";
document.body.style.color = "white";
}
}
// Get and Set User Name
function getName() {
const storedName = localStorage.getItem("name");
if (storedName === null) {
nameInput.textContent = "[Enter Name]";
} else {
nameInput.textContent = storedName;
}
}
function setName() {
const enteredName = nameInput.innerText.trim();
if (enteredName === "") {
nameInput.textContent = "[Enter Name]";
} else {
localStorage.setItem("name", enteredName);
}
}
// Get and Set User Focus
function getFocus() {
const storedFocus = localStorage.getItem("focus");
if (storedFocus === null) {
focusInput.textContent = "[Enter Focus]";
} else {
focusInput.textContent = storedFocus;
}
}
function setFocus() {
const enteredFocus = focusInput.innerText.trim();
if (enteredFocus === "") {
focusInput.textContent = "[Enter Focus]";
} else {
localStorage.setItem("focus", enteredFocus);
}
}
// Event Listeners for Name and Focus Input
nameInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
setName();
nameInput.blur();
}
});
nameInput.addEventListener("blur", setName);
focusInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
setFocus();
focusInput.blur();
}
});
focusInput.addEventListener("blur", setFocus);
// Initialize the Page
function initPage() {
showTime();
setBgGreet();
getName();
getFocus();
}
// Run Initialization
initPage();
});