일반함수 풀이
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>콜백함수-4(일반함수 풀이)</title>
<script src="libs/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
//함수를 시작과 동시에 호출을 하고 있다.
checkCount();
});
//일반함수를 이용한 풀이
function checkCount(){
var count = 0;
$("#btnStart").click(function(){
count++;
if(count >= 5){
alert("완료되었습니다.");
}
});
}
</script>
</head>
<body>
<button id="btnStart">시작</button>
</body>
</html>
콜백함수
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>콜백함수-4(콜백함수 풀이)</title>
<script src="libs/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
//함수를 시작과 동시에 호출을 하고 있다.
checkCount(showAlert);
});
//일반함수(로직처리부분)
function showAlert(){
alert("완료되었습니다.");
}
//콜백함수를 이용한 풀이
function checkCount(callback){
var count = 0;
$("#btnStart").click(function(){
count++;
if(count >= 5){
callback();
}
});
}
</script>
</head>
<body>
<button id="btnStart">시작</button>
</body>
</html>

댓글남기기