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);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章