javascript 中的 setTimeout() 方法是做什么的?
setTimeout()
这是许多计时事件之一。window 对象允许在指定的时间间隔执行代码。此对象提供了 SetTimeout() 以在一段时间后执行函数。它将两个参数作为参数。一个是函数,另一个是时间,它指定在该时间后应执行该函数。
语法
window.setTimeout(function, milliseconds);
示例 1
在以下示例中,传递给 setTimeout() 函数的时间为 2 秒。因此,该函数将在 2 秒后执行并显示如下所示的输出。
<html>
<body>
<p>wait 2 seconds.</p>
<script>
setTimeout(function(){
document.write("Tutorix is the best e-learning platform");
}, 2000);
</script>
</body>
</html>输出
wait 2 seconds Tutorix is the best e-learning platform
示例 2
在以下示例中,传递给 setTimeout() 函数的时间为 3 秒。因此,该函数将在 3 秒后执行并显示如下所示的输出。
<html>
<body>
<p id = "time"></p>
<script>
setTimeout(function(){
document.getElementById("time").innerHTML = "Tutorix is the product of Tutorialspoint";
}, 3000);
</script>
</body>
</html>输出
Tutorix is the product of Tutorialspoint.
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP