ë¸ëźě°ě ě ë°ě´í°ëĽź ě ěĽíęł , ě´ëĽź ëě¤ě ę°ě ¸ë¤ ě¸ ě ěëëĄ íë¤
ěŹę¸°ěë userę° ěëĄęł 욨í ëë§ë¤ ě´ëŚě ě ë Ľíě§ ěěë ëę˛ë ë¸ëźě°ě ę° userě ě´ëŚě 기ěľíëëĄ íë¤
localStorage.setItem(í¤, ę°);
ëĄěťŹ ě¤í 댏ě§ě í¤ ę° ě ěśę°
localStorage.getItem(í¤);
ëĄěťŹ ě¤í 댏ě§ěě í´ëš í¤ě ëěíë ę° ě˝ę¸°
localStorage.removeItem(í¤);
ëĄěťŹ ě¤í 댏ě§ěě í´ëš í¤ ę° ě ě ęą°
localStorage.clear();
ëĄěťŹ ě¤í ëŚŹě§ í몊 ě 체 ě ęą°
<!-- h1ęłź form 모ë 기본ě ěźëĄ ě¨ęš ěí -->
<h1 id="greeting" class="hidden"></h1>
<form id="login-form" class="hidden">
<input required maxlength="15" type="text" placeholder="what is your name?">
<button>Log In</button>
</form>
const loginForm = document.querySelector('#login-form');
const loginInput = document.querySelector('#login-form input');
const greeting = document.querySelector('#greeting');
const HIDDEN_CLASSNAME = 'hidden';
const USERNAME_key = 'username';
function onLoginSubmit(event) {
event.preventDefault();
loginForm.classList.add(HIDDEN_CLASSNAME);
const username = loginInput.value;
localStorage.setItem(USERNAME_key, username);
paintGreetings(username);
}
function paintGreetings(username) {
greeting.innerText = `Hello ${username}`;
greeting.classList.remove(HIDDEN_CLASSNAME);
}
const savedUsername = localStorage.getItem(USERNAME_key);
// í¤ě ëěíë ę°ě´ ěěźëŠ´ ëłěěë nullě´ í ëšëë¤
if (savedUsername === null) {
loginForm.classList.remove(HIDDEN_CLASSNAME);
loginForm.addEventListener('submit', onLoginSubmit);
} else {
paintGreetings(savedUsername);
}
function print() {
console.log('syong');
}
setInterval(print, 1000); // 1ě´ë§ë¤ í ë˛ěŠ ě¤í
setTimeout(print, 1000); // 1ě´ ë¤ě í ë˛ ě¤í
string.padStart(ěľě 길ě´, ę¸¸ě´ ëŻ¸ëŹ ě ěśę°í 돸ě)
string.prototype.padEnd(ěľě 길ě´, ę¸¸ě´ ëŻ¸ëŹ ě ěśę°í 돸ě)
"end".padStart(5, "0"); // 00end
"end".padEnd(5, "0"); // end00
"end".padEnd(3, "0"); // end
padStart()
ë stringěë§ ě¤íí ě ěěźëŻëĄ String() í¨ě
뼟 ě´ěŠí´ ěę°ě ě미íë ěŤěë¤ě 돸ěě´ëĄ ě ëś ë°ężě¤
const clock = document.querySelector('h2');
function getClock() {
const date = new Date();
const hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
clock.innerText = `${hours}:${minutes}:${seconds}`;
}
getClock();
setInterval(getClock, 1000);
ëŞ
ě¸ë¤ě ë°°ě´ ěě object ííëĄ ëě´í´ ëě í Math.random()
ęłź setInterval()
ě ě´ěŠí´ ěźě í ěę° ę°ę˛Šě ëęł ëë¤ěźëĄ ë˝ě í늴ě ëł´ě´ëëĄ í´ěŁźěë¤.
<div id="quote">
<span></span>
<span></span>
</div>
const quotes = [
{
quotes: 'ë¸ëźë¸ëź',
author: 'ë¸ëź'
},
{
quotes: 'ë¸ëźë¸ëźë¸ëźë¸ëź',
author: 'ë¸ëźë¸ëźë¸ëź'
},
// ěëľ (ě´ 5ę° ëŞ
ě¸ ěěą)
];
const quote = document.querySelector('#quote span:first-child');
const author = document.querySelector('#quote span:last-child');
function showQuote() {
const todayQuote = quotes[Math.floor(Math.random() * quotes.length)];
quote.innerText = todayQuote.quotes;
author.innerText = todayQuote.author;
}
showQuote();
setInterval(showQuote, 5000);
quotesě ëšěˇíę˛ ě´ëŻ¸ě§ë¤ě ë°°ě´ ěě ëě´í´ ëě í Math.random()ęłź document.createElement()
, Node.appendChild()
뼟 ě´ěŠí´ documentě body ëě ě´ëŻ¸ě§ëĽź ěśę°í´ 죟ěë¤. ěëĄęł 욨í ëë§ë¤ ě´ëŻ¸ě§ę° ëë¤íę˛ ë°ëë¤.
const images = [
'0.jpg',
'1.jpg',
'2.jpg'
];
const chosenImage = images[Math.floor(Math.random() * images.length)];
const bgImage = document.createElement('img');
bgImage.setAttribute('src', `img/${chosenImage}`);
document.body.appendChild(bgImage);