๐ก intervals : ์ค์ ๋ ์๊ฐ๋ง๋ค ๋งค๋ฒ ํจ์๋ฅผ ์ํํ๊ฒ ์ค์ ํ๋๊ฒ ex) ๋งค 2์ด๋ง๋ค ์คํ์ํค๋ ๋ฌด์ธ๊ฐ.
function askYourName(){
console.log("what's your name?");
}
//์ด ํจ์๋ฅผ ์ฌ๋ฌ๋ฒ ํธ์ถ ํ๊ณ ์ถ์ผ๋ฉด JS์ ์ด๋ฏธ ๋ด์ฅ๋ ๊ธฐ๋ฅ์ ์ฐ๋ฉด๋๋ค.
setInterval(askYourName, 5000);
//ms๋จ์๋ผ 5์ด๋ฉด 5000์ ์ค์ ํ๋ฉด๋๋ค.
๐ก Interval์ ์
๋ฐ์ดํธ๊ฐ ํ์ํ data๋ ์ฃผ์ํธ๊ฐ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๊ธฐ ์ ํฉํ๋ค.
๐ก timeout : ์ผ์ ์๊ฐ ๋ค์ ๊ธฐ๋ฅ์ ๋ฑ ํ๋ฒ๋ง ์ํํ๊ฒ ์ค์ ํ๋๊ฒ
function askYourName(){
console.log("what's your name?");
}
setTimeout(askYourName, 5000)
//5์ด๋ค์ ๋ฑ ํ๋ฒ askYourName()์ด ์คํ๋๋ค.
โฐ ์๊ณ๋ง๋ค๊ธฐ
const clock = document.querySelector("h2#clock");
//html์ id๊ฐ์ผ๋ก ์ฐ๊ฒฐ
function getTime() {
const date = new Date();
clock.innerText = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
}
getTime();
//Interval์ 1์ด๊ฐ๊ฒฉ์ผ๋ก ์
ํ
๋์ด ์๊ธฐ๋๋ฌธ์ ํ์ด์ง๊ฐ reload๋๋ฉด 1์ด๋ฅผ ๊ธฐ๋ค๋ ค์ผํจ.
//ํจ์๋ฅผ ์ฆ๊ฐ ์คํ์ํด์ผ๋ก ๋ฐฉ์ง๊ฐ๋ฅ.
setInterval(getTime, 1000);