當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
JavaScript setTimeout函数
生活随笔
收集整理的這篇文章主要介紹了
JavaScript setTimeout函数
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
setTimeout的作用:延遲一段時(shí)間執(zhí)行某個(gè)函數(shù)。
例如:setTimeout(function(){alert("延遲5秒執(zhí)行...")},5000);
注意:setTimeout()不是循環(huán)執(zhí)行,只執(zhí)行一次。
setTimeout是異步調(diào)用,setTimeout方法與其后的方法同時(shí)執(zhí)行。
例如:setTimeout(function(){alert("延遲5秒執(zhí)行...")},5000);
alert("判斷setTimeout是否是異步執(zhí)行,如果先彈出則表示是異步執(zhí)行。");
? 執(zhí)行結(jié)果可以證明setTimeout是異步的。
使用setTimeout實(shí)現(xiàn)計(jì)時(shí)器的算法:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>setTimeout計(jì)時(shí)器的實(shí)現(xiàn)</title>
<script type="text/javascript" language="javascript">
var time = 0;
function currTimeView(){
document.getElementById("currTime").value = time + "秒";
}
/**開(kāi)始計(jì)時(shí)*/
function startTime(){
time++;
currTimeView();
tid = setTimeout("startTime();",1000);
}
/**開(kāi)始計(jì)時(shí)*/
function stopTime(){
clearTimeout(tid);
}
/**開(kāi)始計(jì)時(shí)*/
function clearTime(){
time = 0;
document.getElementById("currTime").value = "開(kāi)始計(jì)時(shí)...";
}
</script>
</head>
<body>
<input id="currTime" type="text" value="開(kāi)始計(jì)時(shí)..." />
<br />
<input id="start" value="開(kāi)始計(jì)時(shí)" type="button" onclick="startTime()" />
<input id="stop" value="停止計(jì)時(shí)" type="button" onclick="stopTime()" />
<input id="clear" value="清空計(jì)時(shí)" type="button" onclick="clearTime()" />
</body>
</html>
轉(zhuǎn)載于:https://www.cnblogs.com/tovep/articles/setTimeout.html
總結(jié)
以上是生活随笔為你收集整理的JavaScript setTimeout函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: sql 触发器
- 下一篇: UVa 111 - History Gr