什麼是 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.