๐Ÿ“ ์˜ค๋Š˜ ํ•œ ๊ฒƒ

  1. localStorage / string() / string.prototype.padStart()

๐Ÿ“š ๋ฐฐ์šด ๊ฒƒ

1. Log In

4) localStorage

๋ธŒ๋ผ์šฐ์ €์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ , ์ด๋ฅผ ๋‚˜์ค‘์— ๊ฐ€์ ธ๋‹ค ์“ธ ์ˆ˜ ์žˆ๋„๋ก ํ•œ๋‹ค

์—ฌ๊ธฐ์„œ๋Š” 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);
}

2. Clock

1) setInterval() VS setTimeout()

function print() {
  console.log('syong');
}

setInterval(print, 1000); // 1์ดˆ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
setTimeout(print, 1000); // 1์ดˆ ๋’ค์— ํ•œ ๋ฒˆ ์‹คํ–‰

2) string.prototype.padStart()

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);

3. Quotes and Background

1) quotes

๋ช…์–ธ๋“ค์„ ๋ฐฐ์—ด ์•ˆ์— 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);

2) background

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);

โœจ ๋‚ด์ผ ํ•  ๊ฒƒ

  1. ํด๋ก ์ฝ”๋”ฉ ๊ณ„์†
profile
dev log

0๊ฐœ์˜ ๋Œ“๊ธ€

๊ด€๋ จ ์ฑ„์šฉ ์ •๋ณด

Powered by GraphCDN, the GraphQL CDN