본문 바로가기
JS

230104 JS window객체- 인터벌

window객체-기본함수

alert() : 경고창
confirm() : 확인창
setTimeout() : 일정 시간이 지난 후 함수 실행
clearTimeout() : setTimout중지 메서드
setInterval() : 일정 시간마다 함수 반복 실행
clearInterval() : setInterval중지 메서드

 

 

 

setInterval과 clearInterval

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <button class="stop">멈춰</button>

    <script>
        //일정주기마다 동작
        var id =window.setInterval(function() {console.log('실행됨')},1000);//(실행시킬 함수, 인터벌);

        //인터벌 중지
        var stop = document.querySelector(".stop");
        stop.onclick=function(){
            window.clearInterval(id);

        }
        
    </script>
    
</body>
</html>

1초마다 콘솔창 실행. 버튼을 누르면 인터벌이 멈춤

 

'JS' 카테고리의 다른 글

230104 JS window객체와 애니메이션 실습  (0) 2023.01.04
230104 JS window객체-타임아웃  (0) 2023.01.04
230104 JS BOM window.open()  (0) 2023.01.04
230104 JS 날짜객체  (0) 2023.01.04
230104 JS 정규표현식  (0) 2023.01.04