如何将参数传递给 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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP