티스토리 뷰

728x90

 

<!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 id="btn">5초 뒤에 뭔가 나올거야!!!</button>
    <button id="cancel">나오지 않도록 해줄게.. 짜증나</button>

    <script>
        let setTo= null;

        document.getElementById("btn").addEventListener("click",function(){
            // setTimeout자체를 setTo라는 변수의 인자로 받기
            setTo = setTimeout(function() {
                alert(`배고파!!!!!!!!!!!!!!!!!!!!!!!`);
            }, 5000);
        });

        document.getElementById("cancel").addEventListener("click",function(){
            // clearTimeout : setTimeout지우는 함수
            clearTimeout(setTo);
        });
    </script>
</body>

</html>

 

 

 

 

<!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 id="access">3초마다 글 추가됨</button>
    <button id="cancel">글 추가 취소시키기</button>
    <div id="content"></div>

    <script>
        // null로 초기화
        let eventTime = null;


        document.getElementById("access").addEventListener("click",function(){
            eventTime = setInterval(function() {
                const p = document.createElement("p");
                const text = document.createTextNode(`오늘은 해물라면`);
                p.append(text);
        
                document.getElementById("content").append(p);
            }, 3000);
        });

        document.getElementById("cancel").addEventListener("click",function(){
            clearInterval(eventTime);
        });
    </script>
</body>

</html>
728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함