본문 바로가기
HTML, JAVASCRIPT

javascript - setInterval 활용하여 카운트다운 멈추기

by 스노위13 2022. 5. 19.

조건
1. 카운트다운 시작 값은  100입니다.
2. 숫자는 1씩 줄어들며, 줄어드는 시간은 0.1초입니다.  
3. 30이 되면 카운트다운이 멈추도록 하세요.
4. 줄어드는 도중에 마우스를 클릭해도 카운트다운이 멈추도록 하세요.

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
32
33
34
35
36
37
38
39
40
41
42
43
<!DOCTYPE html>
<html>
<head><title>setInterval</title>
<style>
 
span{
        font-size: 50px;
}
    
</style>
 
</head>
<body>
 
<h3>숫자를 100부터 카운트하며 30초가 되면 멈춥니다.</h3>
<hr>
<div>
    <span id="span_1" style="background-color:yellow">100</span>
</div>
 
<script>
 
let span_1 = document.getElementById("span_1");
 
let countS = parseInt(span_1.textContent);
 
let timerID = setInterval("doRotate()"100); 
 
 
function doRotate() {
    span_1.textContent = countS; 
    countS--;
    if(countS < 30){
        clearInterval(timerID); 
    }
    span_1.onclick = function (e) {
    clearInterval(timerID); 
}
}
 
</script>
</body>
</html>
cs

 

결과값

카운트다운 화면 결과

 

댓글