setTimeout()を使用し、時刻をリアルタイムで表示します。

サンプル

サンプルソース

JavaScript + HTML

<form name="setTimeForm">
	<input type="text" size="25" name="setTimeText" ID="setTimeText" />
</form>
<script type="text/javascript">
<!--
	// form の後にスクリプトを記述してください
	day = new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	myFunc();
	function myFunc(){
		acy = (new Date()).getYear();
		act = (new Date()).getMonth() + 1;
		acd = (new Date()).getDate();
		acw = (new Date()).getDay();
		ach = (new Date()).getHours();
		acm = (new Date()).getMinutes();
		acs = (new Date()).getSeconds();
		if(act < 10) act = "0" + act;
		if(acd < 10) acd = "0" + acd;
		if(ach < 10) ach = "0" + ach;
		if(acm < 10) acm = "0" + acm;
		if(acs < 10) acs = "0" + acs;
		document.setTimeForm.setTimeText.value = acy+"/"+act+"/"+acd+" ("+day[acw]+") "+ach+":"+acm+":"+acs;
		setTimeout("myFunc()",1000);
	}
//-->
</script>
カテゴリ: JavaScript 2010/04/20 4:16

コメントをどうぞ