什么是 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>

更新于: 2020 年 6 月 23 日

360 次浏览

开启你的职业生涯

完成课程获取认证

开始
广告
© . All rights reserved.