JavaScript
javaScript-호출 스케줄링
용용it
2023. 7. 5. 02:13
// 호출 스케줄링(Scheduling a function call)
// setTimeout() 함수는 특정 시간이 지난 후에 함수를 호출한다.
setTimeout() => {
console.log('Hello')
}, 2000)
// 2초 뒤에 Hello 출력
const hello = () => {
console.log('Hello')
}
setTimeout(hello, 2000)
// Hello
const hello = () => {
console.log('hello') // hello
}
const timeout = setTimeout(hello, 2000) // 2초 뒤에 hello 함수 호출
const h1El = document.querySelector('h1') // h1 태그를 h1El 상수 선언
h1El.addEventListener('click', () => {
clearTimeout(timeout)
})
// h1El 상수 클릭 이벤트시 clearTimeout(timeout), timeout으로 설정된 지연 실행을 취소
const hello = () => {
console.log('hello'); // hello 출력
};
const timeout = setInterval(hello, 2000); // 2초마다 hello 함수 호출
const h1El = document.querySelector('h1'); // h1 태그를 h1El 상수로 선택
h1El.addEventListener('click', () => {
console.log('Clear!'); // h1 태그 클릭 시 Clear! 출력
clearInterval(timeout);
});
// HTML 문서에서 첫 번째 h1 태그를 선택하고, 해당 태그를 클릭하면 'Clear!'를 콘솔에 출력하고, timeout으로 설정된 반복 실행을 중지