语法
setTimeout(code,millisec)
参数 描述
code 必需。要调用的函数后要执行的 代码串。
millisec 必需。在执行代码前需等待的毫秒数。
提示和注释
提示:setTimeout() 只执行 code 一次。如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout()。
实例:
<html>
<head>
< type=\"text/ \">
function timedMsg()
{
var t=setTimeout(\"alert(\'5 seconds!\')\",5000)
}
</ >
</head>
<body>
<form>
<input type=\"button\" value=\"Display timed alertbox!\"
=\"timedMsg()\">
</form>
<p>Click on the button above. An alert box will be
displayed after 5 seconds.</p>
</body>
</html>
<##ads_in_article_manong##>
如果需要终止定时函数执行,可以使用clearTimeout()。
clearTimeout() 方法可取消由 setTimeout() 方法设置的 timeout。
语法
clearTimeout(id_of_settimeout)
参数 描述
id_of_settimeout 由 setTimeout() 返回的 ID 值。该值标识要取消的延迟执行代码块。
下面的例子每秒调用一次 timedCount() 函 数。您也可以使用一个按钮来终止这个定时消息:
<html>
<head>
< type=\"text/ \">
var c=0
var t
function timedCount(){
document.getElementById(\'txt\').value=c
c=c+1
t=setTimeout(\"timedCount()\",1000)
}
function stopCount(){
clearTimeout(t)
}
</ >
</head>
<body>
<form>
<input type=\"button\" value=\"Start count!\" =\"timedCount()\">
<input type=\"text\" id=\"txt\">
<input type=\"button\" value=\"Stop count!\" =\"stopCount()\">
</form>
</body>
</html> 继续阅读与本文标签相同的文章
上一篇 :
js indexOf 函数用法实例
下一篇 :
js setInterval()的用法实例
-
详解Libra的Move IR编译器漏洞
2026-05-14栏目: 教程
-
人工智能时代的产业投资机会在哪里?交大行业研究院这场论坛有干货!
2026-05-14栏目: 教程
-
大数据的核心技术概念,你了解吗?
2026-05-14栏目: 教程
-
网红机器人泰坦现身大唐不夜城 能与观众聊天
2026-05-14栏目: 教程
-
罗永浩被指责带不动锤子手机 凌晨发文怼网友
2026-05-14栏目: 教程
