• 코드:
​x
 
1
<!DOCTYPE html>
2
<html lang="ko">
3
​
4
<head>
5
    <meta charset="UTF-8">
6
    <title>JavaScript BOM Timer</title>
7
</head>
8
​
9
<body>
10
​
11
    <h1>clearTimeout() 메소드</h1>
12
    <button onclick="startTimeout()">2초 뒤에 현재 시간을 표시합니다!</button>
13
    <button onclick="cancelTimeout()">시간 표시 취소!</button>
14
    <p id="date"></p>
15
        
16
    <script>
17
        var timeoutId;
18
        function startTimeout() {
19
            timeoutId = setTimeout(printCurrentDate, 2000);
20
        }
21
        function cancelTimeout() {
22
            clearTimeout(timeoutId);
23
        }
24
        function printCurrentDate() {
25
            document.getElementById("date").innerHTML += new Date() + "<br>";
26
        }
27
    </script>
28
    
29
</body>
30
​
31
</html>