如何将参数传递给 setTimeout() 回调?
javascriptweb developmentfront end technology更新于 2024/4/18 8:59:00
要将参数传递给 setTimeout() 回调,请使用以下语法 −
setTimeout(functionname, milliseconds, arg1, arg2, arg3...)
以下是参数 −
- functionname − 要执行的函数的函数名称。
- milliseconds − 毫秒数。
- arg1, arg2, arg3 −这些是传递给函数的参数。
示例
您可以尝试运行以下代码以将参数传递给 setTimeout() 回调
<!DOCTYPE html> <html> <body> <button onclick="timeFunction()">Submit</button> <script> function timeFunction() { setTimeout(function(){ alert("5秒后!"); }, 5000); } </script> <p>点击上述按钮,等待5秒。</p> </body> </html>