๋ธ๋ผ์ฐ์ ์ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ , ์ด๋ฅผ ๋์ค์ ๊ฐ์ ธ๋ค ์ธ ์ ์๋๋ก ํ๋ค
์ฌ๊ธฐ์๋ 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);