javascript 中的 setTimeout() 方法是什么?
javascriptobject oriented programmingfront end technology更新于 2025/9/20 22:04:17
setTimeout()
这是众多计时事件之一。window 对象允许以指定的时间间隔执行代码。此对象提供了 SetTimeout(),用于在一定时间后执行函数。它需要两个参数作为参数。一个是函数,另一个是时间,用于指定应在哪个间隔后执行函数。
语法
window.setTimeout(function, milliseconds);
示例 1
在下面的示例中,传递给 setTimeout() 函数的时间为 2 秒。因此,该函数将在两秒后执行,并显示如下所示的输出。
<html>
<body>
<p>wait 2 seconds.</p>
<script>
setTimeout(function(){
document.write("Tutorix is the best e-learning platform");
}, 2000);
</script>
</body>
</html>
输出
等待 2 秒 Tutorix 是最好的电子学习平台
示例 2
在下面的例子中,传递给 setTimeout() 函数的时间为 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.
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

