什么是JavaScript中的回调函数?

当一个函数传递给另一个函数时,它称为回调函数。它比调用传递的函数更重要。

示例

您可以尝试运行以下代码以了解如何使用回调函数-

<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>