JavaScript - Date对象
Date对象
<html>
<head>
<title>Date对象</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
function showCurrentTime(){
var today=new Date();
//document.writeln(today);
var year=today.getFullYear();
var month=today.getMonth()+1;
month=checkTime(month);
var date=today.getDate();
date=checkTime(date);
var hour=today.getHours();
hour=checkTime(hour);
var minute=today.getMinutes();
minute=checkTime(minute);
var second=checkTime(today.getSeconds());
var num=today.getDay();
var weekday;
switch(num){
case 0:
weekday="星期天";
break;
case 1:
weekday="星期一";
break;
case 2:
weekday="星期二";
break;
}
$("currentTime").innerHTML=year+"年"+month+"月"+date+"日 "+hour+":"+minute+":"+second+" "+weekday;
}
setInterval("showCurrentTime()",1000);
function checkTime(i){
if(i<10){
i="0"+i;
}
return i;
}
var timer;
function show(){
document.getElementById("info").innerHTML+="Hello World !<br/>";
timer=setTimeout("show()",500); //开启一个计时器,并返回此计时器
}
//定时器setTimeout()
//setTimeout("show()",5000); //5秒后调用show()函数
//window.onload=show;
//
function getTitle(){
//alert(document.getElementById("title").innerHTML);
document.getElementById("title").innerHTML="你的我的大西瓜";
}
function startTimer(){
show();
}
function stopTimer(){
clearTimeout(timer);
}
//定时器setInterval()
function show2(){
document.getElementById("info").innerHTML+="Hello World !<br/>";
}
function startTimer2(){
timer=setInterval("show2()",500);
}
function stopTimer2(){
clearInterval(timer);
}
//简化通过ID获取元素
function $(id){
return document.getElementById(id);
}
</script>
</head>
<body>
<div id="currentTime"></div>
<hr/>
使用setTimeout()实现
<input type="button" value="走你...." onclick="startTimer()"/>
<input type="button" value="停..." onclick="stopTimer()"/><br/>
使用setInterval()实现
<input type="button" value="走你...." onclick="startTimer2()"/>
<input type="button" value="停..." onclick="stopTimer2()"/>
<div id="info"></div>
<h2 id="title">你是我的小呀小苹果</h2>
<input type="button" value="获取标题" onclick="getTitle()"/>
</body>
</html>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。