currentUser
키 값이 없는 경우currentUser
localStorage에 저장<section class="question">
<input type="text" class="questionInput" placeholder="What's Your Name">
</section>
<section class="clockAndTodo">
<div class="clock">
<span class="time"></span>
<span class="name"></span>
</div>
<div class="toDo">
<input type="text" class="todoText" placeholder="Write To Do here">
<ul class="List">
<li class="item">
<input type="checkbox" class="checkbox">
<span class="content">to do</span>
<button class="delete">
<i class="far fa-times-circle"></i>
</button>
</li>
</ul>
</div>
</section>
const input = document.querySelector(".questionInput");
const USER_LS = "currentUser";
const SHOW = "showing";
const clockAndTodo = document.querySelector(".clockAndTodo");
const nameSpan = document.querySelector(".name");
const question = document.querySelector(".question");
function printContent(currentUser) {
question.classList.remove("showing");
clockAndTodo.classList.add("showing");
nameSpan.innerText = `Hello ${currentUser}`;
}
function saveCurrentUser() {
input.addEventListener("keydown", (e) => {
if (e.keyCode === 13) {
const value = input.value;
localStorage.setItem(USER_LS, value);
printContent(value);
}
});
}
function showQuestion() {
question.classList.add(SHOW);
}
function loadclockAndTodo() {
const currentUser = localStorage.getItem(USER_LS);
if (currentUser === null) {
showQuestion();
saveCurrentUser();
} else {
printContent(currentUser);
}
}
loadclockAndTodo();
localStorage : local에 저장되는 저장소이다.
주의할 점 : 오직 문자열 데이터만 지원한다!