JavaScript学习笔记之定时器
定时器1
用以指定在一段特定的时间后执行某段程序。
setTimeout():
格式:[定时器对象名=]setTimeout(“<表达式>”,毫秒)
功能:执行<表达式>一次。
例子:
<!DOCTYPEhtml>
<html>
<head>
<title>timer1.html</title>
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="thisismypage">
<metahttp-equiv="content-type"content="text/html;charset=UTF-8">
<!--<linkrel="stylesheet"type="text/css"href="./styles.css">-->
<scripttype="text/javascript">
functioncount()
{
setTimeout("alert('执行成功!')",7000);
}
</script>
</head>
<body>
<inputtype="button"value="点击我啊"onclick="count();">
</body>
</html>
定时器2
以一定的时间为间隔,不断地重复执行表达式。
setInterval():
格式:[定时器对象名=]setInterval(“<表达式>”,毫秒)
功能:重复执行<表达式>,直至窗口、框架被关闭或执行clearInterval。
clearInterval():
格式:clearInterval(定时器对象名)
功能:终止定时器
例子:
<!DOCTYPEhtml>
<html>
<head>
<title>timer2.html</title>
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="thisismypage">
<metahttp-equiv="content-type"content="text/html;charset=UTF-8">
<!--<linkrel="stylesheet"type="text/css"href="./styles.css">-->
<scripttype="text/javascript">
varsec=0;
vartimer=setInterval("count();",1000);//页面加载的时候即开始计时
functioncount()
{
document.getElementById("num").innerHTML=sec++;
}
functionstopCount()
{
clearInterval(timer);//停止定时器的运行
}
</script>
</head>
<body>
<fontcolor="red"id="num">0</font>
<inputtype="button"value="停止"onclick="stopCount();">
</body>
</html>
以上就是本文的全部内容了,希望大家能够喜欢