Javascript第五章倒计时第二课
HTML DOM setTimeout() 方法
ECMAScript参考:https://blog.****.net/qq_30225725/article/details/88621180
DOM参考:Javascript第五章window对象的事件常用方法第三课:https://blog.****.net/qq_30225725/article/details/88624216
BOM参考:Javascript第五章history对象第四课:https://blog.****.net/qq_30225725/article/details/88624376
Javascript第五章location对象第五课
https://blog.****.net/qq_30225725/article/details/88624613
更多免费教学文章请关注这里
<html>
<head>
<script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('5 seconds!')",5000)
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed alertbox!"
onClick="timedMsg()">
</form>
<p>Click on the button above. An alert box will be
displayed after 5 seconds.</p>
</body>
</html>
HTML DOM setTimeout() 方法
循环的
<html>
<head>
<script type="text/javascript">
function timedMsg()
{
var t=setTimeout("alert('5 seconds!')",5000)
}
</script>
</head>
<body>
<form>
<input type="button" value="Display timed alertbox!"
onClick="timedMsg()">
</form>
<p>Click on the button above. An alert box will be
displayed after 5 seconds.</p>
</body>
</html>
停止计时,方法是一一对应的
HTML DOM clearInterval() 方法
下面这个例子将每隔 50 毫秒调用 clock() 函数。您也可以使用一个按钮来停止这个 clock:
<html>
<body>
<input type="text" id="clock" size="35" />
<script language=javascript>
var int=self.setInterval("clock()",50)
function clock()
{
var t=new Date()
document.getElementById("clock").value=t
}
</script>
</form>
<button onclick="int=window.clearInterval(int)">
Stop interval</button>
</body>
</html>
HTML DOM clearTimeout() 方法
既可以停止一次性计时器也可以停止周期性计时器
实例
下面的例子每秒调用一次 timedCount() 函数。您也可以使用一个按钮来终止这个定时消息:
<html>
<head>
<script type="text/javascript">
var c=0
var t
function timedCount()
{
document.getElementById('txt').value=c
c=c+1
t=setTimeout("timedCount()",1000)
}
function stopCount()
{
clearTimeout(t)
}
</script>
</head>
<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">
</form>
</body>
</html>
用 setTimeout一次性计时器,也能实现周期效果
直接在参数里写个匿名函数: