함수가 다른 함수에 전달되면 이를 콜백 함수라고 합니다. 전달된 함수를 호출하는 것보다 이 함수를 넘어갑니다.
예시
다음 코드를 실행하여 콜백 함수 사용 방법을 배울 수 있습니다. −
<html>
<head>
<script>
var callback = function(myCallback) {
setTimeout(function() {
myCallback();
}, 5000);
};
document.write("First is displayed");
document.write("<br>Second is displayed");
callback(function() {
document.write("This is Callback function");
});
document.write("<br>Last is displayed");
</script>
</head>
</html>