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


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

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

以下為引數 -

  • 函式名 - 要執行的函式的函式名。
  • 毫秒 - 毫秒數。
  • 引數 1、引數 2、引數 3 - 這些是傳遞給函式的引數。

示例

您可以嘗試執行以下程式碼,將引數傳遞給 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>

更新於: 12-6 月-2020

2K+ 瀏覽量

開啟您的 職業生涯

完成課程並獲得證書認證

開始
廣告
© . All rights reserved.