码迷,mamicode.com
首页 > Web开发 > 详细

js时钟

时间:2014-06-21 19:35:19      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:js时钟

<!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 = "text" id = "time" />		
			<input type = "button" value = "启动时钟" onclick = "start()"/>
			<input type = "button" value = "关闭时钟" onclick = "stop()"/>
		</form>
	</body>
</html>


//js.js
var timer;
function start(){
	timer = window.setInterval(time,1000);///设置定时器  第一个参数表示要干什么  第二个参数表示时间间隔   返回一个时钟,用于区别其他时钟以及关闭
}
function time(){
	var date = new Date();///获得一个Date对象
	var datestr = date.toLocaleTimeString();///取得时间  没有年月日  
	document.getElementById("time").value = datestr;
}
function stop(){
	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

var time1;
function waithello(){
	time1 = window.setTimeout("alert(‘hello‘)",3000);///window.setTimeout(f,时间) 第一个参数是要执行的内容 可以是一个方法,第二个是间隔的时间,
	//用于将一个操作延迟一定的时间  执行一次
}
function cancel(){
	//用于当不想将操作延迟时  可以打断延迟,取消延迟
	window.clearTimeout(time1);
}


js时钟,布布扣,bubuko.com

js时钟

标签:js时钟

原文地址:http://nobelking.blog.51cto.com/7607139/1429154

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!