如何將引數傳遞給 setTimeout() 回撥?


要將引數傳遞給 setTimeout() 回撥,請使用以下語法 −

setTimeout(functionname, milliseconds, arg1, arg2, arg3...)

以下是引數 −

  • 函式名 − 要執行的函式的函式名。
  • 毫秒數 − 毫秒數。
  • arg1、arg2、arg3 − 這些是傳遞給函式的引數。

例項

你可以嘗試執行以下程式碼,將引數傳遞給 setTimeout() 回撥

線上演示

<!DOCTYPE html>
<html>
   <body>
      <button onclick="timeFunction()">Submit</button>
      <script>
         function timeFunction() {
            setTimeout(function(){ alert("After 5 seconds!"); }, 5000);
         }
      </script>
   <p>Click the above button and wait for 5 seconds.</p>
   </body>
</html>

更新於:2020 年 6 月 12 日

2K+ 瀏覽

開啟您的 職業 生涯

透過完成該課程獲得認證

立即開始
廣告
© . All rights reserved.