JS 定时器 (setInterval 和 setTimeout 函数)

本文提供了两个使用JavaScript实现定时刷新显示当前时间的例子。第一个例子通过表单输入框展示时间,第二个例子则在页面上直接显示时间及星期信息。两个示例均包括开始和停止时间更新的功能。
None.gifDEMO1:
None.gif
<html>
None.gif
None.gif
<head>
None.gif
ExpandedBlockStart.gifContractedBlock.gif
<script language="JavaScript">dot.gif
InBlock.gif
<!--
InBlock.gif
InBlock.gif
function Refresh()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
var newDateObj = new Date();
InBlock.gif document.form1.txt_Time.value
=newDateObj.toLocaleString();//使用当前区域设置并已被转换为字符串
ExpandedSubBlockEnd.gif
}

InBlock.gif
InBlock.gif
var MyInterval=setInterval("Refresh()",1000);
InBlock.gif
InBlock.gif
function Stop()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
if(event.srcElement.value=="STOP")
InBlock.gif 
//if(document.form1.btn_Stop.value=="STOP")
ExpandedSubBlockStart.gifContractedSubBlock.gif
 dot.gif{
InBlock.gif  clearInterval(MyInterval);
InBlock.gif  document.form1.btn_Stop.value
="START";
ExpandedSubBlockEnd.gif }

InBlock.gif 
else if(event.srcElement.value=="START")
InBlock.gif 
//else if(document.form1.btn_Stop.value=="START")
ExpandedSubBlockStart.gifContractedSubBlock.gif
 dot.gif{
InBlock.gif  MyInterval
=setInterval("Refresh()",1000);
InBlock.gif  document.form1.btn_Stop.value
="STOP";
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
function Clear()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
if(event.keyCode==8)//退格键
ExpandedSubBlockStart.gifContractedSubBlock.gif
 dot.gif{
InBlock.gif  event.srcElement.value
="";
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
//-->
None.gif
</script>
None.gif
</head>
None.gif
None.gif
None.gif
<body>
None.gif
None.gif
<form name="form1">
None.gif当前时间:
<input type=text name="txt_Time" size=30 onkeydown="Clear()">
None.gif
<input type="button" name="btn_Stop" value="STOP" onClick="Stop()">
None.gif
None.gif
None.gif
</form>
None.gif
None.gif
</body>
None.gif
None.gif
</html>
None.gif
None.gif
None.gifDEMO2:
ExpandedBlockStart.gifContractedBlock.gif
<SCRIPT>dot.gif
InBlock.gif
InBlock.gif
function Refresh()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif Time.innerHTML
=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());
InBlock.gif setTimeout(
"Refresh()",1000);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
var timer=setTimeout("Refresh()",1000);
InBlock.gif
InBlock.gif
function Stop()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
if(event.srcElement.value=="STOP")
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  clearTimeout(timer);
InBlock.gif  document.all.btn_Stop.value
="START";
ExpandedSubBlockEnd.gif }

InBlock.gif 
else if(event.srcElement.value=="START")
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  timer
=setTimeout("Refresh()",1000);
InBlock.gif  document.all.btn_Stop.value
="STOP";
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif
None.gif
</SCRIPT>
None.gif
None.gif
<body>
None.gif
<DIV style="background-color:#eeeeee" id="Time"></DIV>
None.gif
<input type="button" name="btn_Stop" value="STOP" onClick="Stop()">
None.gif
None.gif
<button name='btnTest' onclick="javascript:alert(this.name)">测试</button>
None.gif
</body>
None.gif
None.gif

转载于:https://www.cnblogs.com/ipusr/archive/2007/04/03/697814.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值