如何将参数传递给 setTimeout() 回调?
javascriptweb developmentfront end technology更新于 2026/2/5 14:04:17
要将参数传递给 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>
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

