您的位置:首页 > 其它

setInterval()和setTimeout()

2015-08-20 15:06 239 查看

window.setInterval()方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。

window.clearInterval()方法可取消setInterval()window.clearInterval()

方法的参数必须是由setInterval()返回的ID值。


实时显示日期:

vari=0;

functionabc()
{
varmytime=newDate();
varmydiv=document.getElementById('abc');
mydiv.innerText=mytime.toLocaleString();
if(++i==5)
{
window.clearInterval(res);
}

}

varres=window.setInterval("abc()",1000);



您也可以使用一个按钮来打开定时器和关闭定时器

<!DOCTYPEhtml>
<htmllang="en">
<head>
<metahttp-equiv="content"content="text/html;charset=utf-8">
<scripttype="text/javascript"charset="utf8">
vararr;
functionw()
{

arr=window.setInterval("clock()");

}
functionclock()
{
vart=newDate();
varwang=document.getElementById("clock");
wang.innerText=t.toLocaleString();
}
</script>
</head>
<body>

当前时间是:<spanid="clock"></span>

<inputonclick="window.clearInterval(arr)"type="button"value="停止计时器">
<inputonclick="w()"type="button"value="开始计时器">
</body>
</html>



利用定时器数秒:

<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<scripttype="text/javascript"charset="utf-8">
vari=10;
functionCountTime()
{
i--;
mybut.value="同意"+i;
if(i==0)
{
//让这个按钮可用
mybut.disabled=false;
window.clearInterval(mytimer);
mybut.value="同意";
}
}
varmytimer=window.setInterval("CountTime()",1000);

</script>
</head>
<body>
<inputtype="button"disabled="true"value="同意10"id="mybut"><br/>
</body>
</html>



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
章节导航