標籤:javascript function 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>
JavaScript - Date對象