浅析javascript 定时器
setTimeout()--用于指定在一段特定的时间后执行某段程序。
格式:
[定时器对象名=]setTimeout(“<表达式>”,毫秒数);
功能:执行<表达式>一次。
其中表达式是字符串,可以使任意javascript语句
<html>
<head>
<scripttype="text/javascript">
//5秒之后执行alert
functioncount(){
setTimeout("alert('执行成功');",5000);
}
</script>
</head>
<body>
<inputtype="button"value="执行"onclick="count()">
</body>
</html>
setInterval()—重复执行<表达式>,直至窗口、框架被关闭或执行clearInterval
格式:[定时器对象名=]setInterval(“<表达式>”,毫秒)
clearInterval()终止定时器
格式:clearInterval(定时器对象名)
<html>
<head>
<scripttype="text/javascript">
varsec=0;
vartimeId=setInterval("count();",1000);
functioncount(){ document.getElementById("num").innerHTML=sec++; }
functionstopCount(){ clearInterval(timeId); } </script> </head> <body> <fontstyle="color:red"id="num">0</font>秒钟<inputtype="button"value="停止"onclick="stopCount();"> </body> </html>
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语