|
| 1 | +// DOM Elements |
| 2 | +const time = document.getElementById('time'), |
| 3 | + greeting = document.getElementById('greeting'), |
| 4 | + name = document.getElementById('name'), |
| 5 | + focus = document.getElementById('focus'); |
| 6 | + |
| 7 | +// Options |
| 8 | +const showAmPm = true; |
| 9 | + |
| 10 | +// Show Time |
| 11 | +function showTime() { |
| 12 | + let today = new Date(), |
| 13 | + hour = today.getHours(), |
| 14 | + min = today.getMinutes(), |
| 15 | + sec = today.getSeconds(); |
| 16 | + |
| 17 | + // Set AM or PM |
| 18 | + const amPm = hour >= 12 ? 'PM' : 'AM'; |
| 19 | + |
| 20 | + // 12hr Format |
| 21 | + hour = hour % 12 || 12; |
| 22 | + |
| 23 | + // Output Time |
| 24 | + time.innerHTML = `${hour}<span>:</span>${addZero(min)}<span>:</span>${addZero( |
| 25 | + sec |
| 26 | + )} ${showAmPm ? amPm : ''}`; |
| 27 | + |
| 28 | + setTimeout(showTime, 1000); |
| 29 | +} |
| 30 | + |
| 31 | +// Add Zeros |
| 32 | +function addZero(n) { |
| 33 | + return (parseInt(n, 10) < 10 ? '0' : '') + n; |
| 34 | +} |
| 35 | + |
| 36 | +// Set Background and Greeting |
| 37 | +function setBgGreet() { |
| 38 | + let today = new Date(), |
| 39 | + hour = today.getHours(); |
| 40 | + |
| 41 | + if (hour < 12) { |
| 42 | + // Morning |
| 43 | + document.body.style.backgroundImage = "url('https://i.ibb.co/rbmx2RK/morning.jpg')"; |
| 44 | + greeting.textContent = 'Good Morning, '; |
| 45 | + } else if (hour < 18) { |
| 46 | + // Afternoon |
| 47 | + document.body.style.backgroundImage = "url('https://i.ibb.co/wKtVjYM/night.jpg')"; |
| 48 | + greeting.textContent = 'Good Afternoon, '; |
| 49 | + } else { |
| 50 | + // Evening |
| 51 | + document.body.style.backgroundImage = "url('https://i.ibb.co/JQRJ91J/afternoon.jpg')"; |
| 52 | + greeting.textContent = 'Good Evening, '; |
| 53 | + document.body.style.color = 'white'; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// Get Name |
| 58 | +function getName() { |
| 59 | + if (localStorage.getItem('name') === null) { |
| 60 | + name.textContent = '[Enter Name]'; |
| 61 | + } else { |
| 62 | + name.textContent = localStorage.getItem('name'); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// Set Name |
| 67 | +function setName(e) { |
| 68 | + if (e.type === 'keypress') { |
| 69 | + // Make sure enter is pressed |
| 70 | + if (e.which == 13 || e.keyCode == 13) { |
| 71 | + localStorage.setItem('name', e.target.innerText); |
| 72 | + name.blur(); |
| 73 | + } |
| 74 | + } else { |
| 75 | + localStorage.setItem('name', e.target.innerText); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// Get Focus |
| 80 | +function getFocus() { |
| 81 | + if (localStorage.getItem('focus') === null) { |
| 82 | + focus.textContent = '[Enter Focus]'; |
| 83 | + } else { |
| 84 | + focus.textContent = localStorage.getItem('focus'); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// Set Focus |
| 89 | +function setFocus(e) { |
| 90 | + if (e.type === 'keypress') { |
| 91 | + // Make sure enter is pressed |
| 92 | + if (e.which == 13 || e.keyCode == 13) { |
| 93 | + localStorage.setItem('focus', e.target.innerText); |
| 94 | + focus.blur(); |
| 95 | + } |
| 96 | + } else { |
| 97 | + localStorage.setItem('focus', e.target.innerText); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +name.addEventListener('keypress', setName); |
| 102 | +name.addEventListener('blur', setName); |
| 103 | +focus.addEventListener('keypress', setFocus); |
| 104 | +focus.addEventListener('blur', setFocus); |
| 105 | + |
| 106 | +// Run |
| 107 | +showTime(); |
| 108 | +setBgGreet(); |
| 109 | +getName(); |
| 110 | +getFocus(); |
0 commit comments