เมื่อฟังก์ชันถูกส่งไปยังฟังก์ชันอื่น จะเรียกว่าฟังก์ชันเรียกกลับ มันใช้ฟังก์ชันนี้มากกว่าการเรียกใช้ฟังก์ชันที่ส่งผ่าน
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อเรียนรู้วิธีทำงานกับฟังก์ชันการโทรกลับ -
<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>