영상
개념
<!DOCTYPE html>
<html lang="ko">
<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>
<style>
#bar {
width: 100%;
height: 30px;
background-color: lightgray;
}
#progress {
width: 10px;
height: 30px;
background-color: yellowgreen;
}
</style>
</head>
<body>
<div id="bar">
<div id="progress"></div>
</div>
<!-- 버튼 이벤트가 한번만 실행되도록 설정 -->
<button onclick="progressFunction();this.onclick=null;">시작</button>
<script>
let width = 0;
let id = null;
function progressFunction() {
width = 0;
id = setInterval(move, 50);
}
function move() {
let element = document.getElementById("progress");
if(width == 100) {
clearInterval(id);
} else {
width++;
element.style.width = width + "%";
}
console.log(width);
}
</script>
</body>
</html>
Java
복사