js时钟
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Strict//EN"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><metahttp-equiv="content-type"content="text/html;charset=utf-8"/><scriptsrc="js.js"type="text/javascript"language="javascript"></script></head><body><form><inputtype="text"id="time"/><inputtype="button"value="启动时钟"onclick="start()"/><inputtype="button"value="关闭时钟"onclick="stop()"/></form></body></html>//js.jsvartimer;functionstart(){timer=window.setInterval(time,1000);///设置定时器第一个参数表示要干什么第二个参数表示时间间隔返回一个时钟,用于区别其他时钟以及关闭}functiontime(){vardate=newDate();///获得一个Date对象vardatestr=date.toLocaleTimeString();///取得时间没有年月日document.getElementById("time").value=datestr;}functionstop(){window.clearInterval(timer);///关闭时钟参数为设置时钟时的返回值}
另一时钟 用于只执行一次的时钟
//jsdemo.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv = "content-type" content="text/html;charset=utf-8"/>
<script src="js.js" type="text/javascript" language="javascript">
</script>
</head>
<body>
<form>
<input type = "button" value = "启动时钟" onclick = "waithello();"/>
<a href = "javascript:cancel();">取消</a>
</form>
</body>
</html>
//js.js
vartime1;functionwaithello(){time1=window.setTimeout("alert('hello')",3000);///window.setTimeout(f,时间)第一个参数是要执行的内容可以是一个方法,第二个是间隔的时间,//用于将一个操作延迟一定的时间执行一次}functioncancel(){//用于当不想将操作延迟时可以打断延迟,取消延迟window.clearTimeout(time1);}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。