getTime(): ํ์ฌ ์๊ฐ์ ๊ตฌํ๊ธฐ ์ํ ํจ์- Date ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ํ์ฌ ์๊ฐ ๊ตฌํ๊ธฐ
- clockHours, clockMinutes, clockSeconds์ ํ ์คํธ๋ฅผ ํ์ฌ ์๊ฐ์ผ๋ก ๋ณ๊ฒฝ
- ์ผํญ ์กฐ๊ฑด ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด ํ ์๋ฆฟ์์ผ ๊ฒฝ์ฐ ์์ '0'์ ๋ถ์ฌ์ค
function getTime() {
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
clockHours.innerText = `${hours < 10 ? `0${hours}` : hours}:`;
clockMinutes.innerText = `${minutes < 10 ? `0${minutes}` : minutes}:`;
clockSeconds.innerText = `${seconds < 10 ? `0${seconds}` : seconds}`;
}setInterval(getTime, 1000);: 1์ด๋ง๋ค getTime ํจ์๊ฐ ์คํ๋๋๋ก ์ค์
getWeather(): ๋ ์จ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ ์ํ ํจ์- Fetch API๋ฅผ ์ฌ์ฉํด openWeatherMap์์ ์ ๊ณตํ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ด
- weather icon์ ์ด๋ฆ์ ํด๋์ค ๊ท์น์ผ๋ก ์ก์๋ฌ์ ํ์ฌ ๋ ์จ์ ๋ง๋ ์์ด์ฝ์ ์ถ๋ ฅ
ex).weather-01d - ์์ด์ฝ ์ด๋ฏธ์ง๋ ์คํ๋ผ์ดํธ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฉ
.weather-icon { position: absolute; left: -70px; display: block; width: 60px; height: 60px; background: url(img/weather-icon.svg) no-repeat; background-size: cover; } .weather-icon.weather-01d { background-position: 0 0; }
function getWeather(lat, lon) {
fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`
)
.then(function(response) {
return response.json();
})
.then(function(json) {
const temperature = Math.floor(json.main.temp);
const weatherIcon = json.weather[0].icon;
weatherIconClass.classList.add(`weather-${weatherIcon}`);
const place = json.name;
weather.innerText = `${temperature}ยฐ`;
weatherLocation.innerText = place;
});
}-
๊ทธ ์ธ ํจ์๋ค
ํจ์๋ช ๊ธฐ๋ฅ askForCoords()ํ์ฌ ์์น์ ํฌ์ง์ ๊ฐ์ ์ ๋ฌ handleGeoSuccess()ํฌ์ง์ ๊ฐ์ ์ฑ๊ณต์ ์ผ๋ก ๊ฐ์ ธ์์ ๋ ์๋, ๊ฒฝ๋๊ฐ์ ๊ตฌํจ handleGeoError()ํฌ์ง์ ๊ฐ์ ๊ฐ์ ธ์ค์ง ๋ชปํ์ ๋ console์ ๋ฉ์์ง๋ฅผ ๋์ saveCoords()localStorage์ ์๋, ๊ฒฝ๋๊ฐ์ ์ ์ฅ loadCoords()localStorage์ ์ ์ฅ๋ ์๋, ๊ฒฝ๋๊ฐ์ด ์๋ค๋ฉด ์ด๋ฅผ ๊ฐ์ ธ์ ๋ ์จ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด askForCoords()๋ฅผ ํธ์ถ
| ํจ์๋ช | ๊ธฐ๋ฅ |
|---|---|
askForName() |
form์ .showing ํด๋์ค๋ฅผ ์ถ๊ฐํด display:block ์ํ๋ก ๋ง๋ค์ด์ฃผ๊ณ , ๊ฐ์ ์
๋ ฅํ ๊ฒฝ์ฐ handleSubmit()ํจ์๋ฅผ ํธ์ถ |
handleSubmit() |
input์ ๊ฐ(์ฌ์ฉ์ ์ด๋ฆ)์ ๊ตฌํด paintGreeting()๊ณผ saveName()ํจ์๋ก ๋ณด๋ด์ค |
paintGreeting() |
form์ display:none์ํ๋ก ๋ง๋ค๊ณ ํ๋ฉด์ ์ฌ์ฉ์ ์ด๋ฆ์ ์ถ๋ ฅ |
saveName() |
localStorage์ ์ฌ์ฉ์ ์ด๋ฆ์ ์ ์ฅ |
loadName() |
localStorage์ ์ ์ฅ๋ ์ฌ์ฉ์ ์ด๋ฆ์ด ์๋ค๋ฉด ์ด๋ฅผ ์ถ๋ ฅํ๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด askForName()์ ํธ์ถ |
getUUID(): UUID๋ฅผ ์ฌ์ฉํด To-do List์ ๊ฐ ํญ๋ชฉ์ ๊ณ ์ id๋ฅผ ๋ถ์ฌUUID : ๋ฒ์ฉ ๊ณ ์ ์๋ณ์(Universally Unique IDentifier)๋ ์ํํธ์จ์ด ๊ตฌ์ถ์ ์ฐ์ด๋ ์๋ณ์ ํ์ค์ด๋ค. UUID ํ์ค์ ๋ฐ๋ผ ์ด๋ฆ์ ๋ถ์ฌํ๋ฉด ์ค์ ์ฌ์ฉ์์ ์ค๋ณต๋ ๊ฐ๋ฅ์ฑ์ด ๊ฑฐ์ ์๋ค๊ณ ์ธ์ ๋๊ธฐ ๋๋ฌธ์ ๋ง์ด ์ฌ์ฉ๋๊ณ ์๋ค. ํ์ค ํ์์์ UUID๋ 32๊ฐ์ ์ญ์ก์ง์๋ก ํํ๋๋ฉฐ ์ด 36๊ฐ ๋ฌธ์(32๊ฐ ๋ฌธ์์ 4๊ฐ์ ํ์ดํ)๋ก ๋ 8-4-4-4-12๋ผ๋ 5๊ฐ์ ๊ทธ๋ฃน์ ํ์ดํ์ผ๋ก ๊ตฌ๋ถํ๋ค.
function getUUID() {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(c) {
var r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 3) | 8;
return v.toString(16);
});
}checkedToDos(): checkbox์ ์ฒดํฌ ์ฌ๋ถ๋ฅผ ํ์ธ ํ, ์ฒดํฌ๋ ๋ฆฌ์คํธ์ .checked ํด๋์ค๋ฅผ ์ถ๊ฐํด label์ ์ด๋ฏธ์ง ๋ณ๊ฒฝ- checkbox์ ์ํ๊ฐ ๋ณํ ๋๋ง๋ค checked ๊ฐ(true/false)์ localStorage์ ์ ์ฅ
.toDoList li label {
display: inline-block;
width: 12px;
height: 12px;
margin-right: 10px;
background: url(img/checkbox.svg);
cursor: pointer;
}
.toDoList li label.checked {
background: url(img/checked.svg);
}-
๊ทธ ์ธ ํจ์๋ค
ํจ์๋ช ๊ธฐ๋ฅ handleSubmit()To-do List๋ฅผ ์ ๋ ฅํ๋ฉด paintToDo()ํจ์๋ก ์ ๋ ฅ๋ฐ์ ๊ฐ์ ์ ๋ฌpaintToDo()li๋ฅผ ์์ฑํด ์ ๋ ฅ๋ฐ์ To-do List๋ฅผ ํ๋ฉด์ ์ถ๋ ฅ, 'text, id, checked'๊ฐ์ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๋ฅผ ์์ฑํด localStorage์ ์ ์ฅ, checkedToDos()ํธ์ถdeleteToDo()del ๋ฒํผ์ ๋ถ๋ชจ li๋ฅผ ์ฐพ์ ์ญ์ ํ, localStorage์ ์ ์ฅ๋ id์ ํด๋น li์ id๋ฅผ ๋น๊ตํด ๊ฐ์ง ์์ ๊ฒฝ์ฐ์๋ง saveToDos()๋ก ์ ๋ฌsaveToDos()localStorage์ To-do List ๋ฐฐ์ด ์ ์ฅ loadToDos()localStorage์ ์ ์ฅ๋ To-do List๊ฐ ์๋ค๋ฉด ์ด๋ฅผ ์ถ๋ ฅ
| ํจ์๋ช | ๊ธฐ๋ฅ |
|---|---|
getRandom() |
Math ๊ฐ์ฒด๋ฅผ ์ด์ฉํด 1~6 ์ฌ์ด์ ๋๋ค ์ซ์ ์์ฑ |
paintImage() |
์ ๋ฌ๋ฐ์ ์์์ ์ซ์๋ฅผ ์ด์ฉํด ํด๋์ค ๊ท์น์ ๋ถ์ฌํด์ ๋๋ค์ผ๋ก ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์ถ๋ ฅ |