글
디지탈 시계
안녕하세요?
오늘은 디지탈 시계소스입니다. 화면에 오전,오후,현재시간을 초까지 나타나게 하는 건데요. 소스를 보시죠.
<span id=tick2></span>
<script>
<!--
// 시계를 보여주는 스크립트 입니다
function show2(){
if (!document.all&&!document.getElementById)
return
thelement=document.getElementById? document.getElementById
("tick2"): document.all.tick2
var Digital=new Date()
var hours=Digital.getHours()
var minutes=Digital.getMinutes()
var seconds=Digital.getSeconds()
var dn="오후"
if (hours<12)
dn="오전"
if (hours>12)
hours=hours-12
if (hours==0)
hours=12
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
var ctime=dn+" " +hours+":"+minutes+":"+seconds
thelement.innerHTML="<b style='font-
size:12;color:black;'>"+ctime+"</b>"
// 글자의 색상을 수정할 수 있습니다
setTimeout("show2()",1000)
// 1초(1000)마다 갱신하여 초단위로 보여줍니다
}
window.onload=show2
//-->
</script>
대충보면 Digital 날짜객체에서 시,분,초를 구해서 ctime에 저장하고 setTimeout을 이용해서 1초마다(1000은 1초를 나타냅니다) 함수를 호출해서 화면에 시간을 표시하게 하는 것입니다. 자세한 것은 스스로 공부해 보시기 바랍니다. 그럼 다음시간에 또 만나요~ 참 실행하면 그냥 오전 오후 나오고 시:분:초 이렇게 나옵니다. 한번 실행해 보시길~
'Java Script' 카테고리의 다른 글
버튼누르면 메시지 출력 (0) | 2011.10.04 |
---|---|
나이입력받아서 처리 (0) | 2011.10.02 |
랜덤으로 사이트 이동하기 (0) | 2011.09.25 |
즐겨찾기에 추가하는 소스코드 (0) | 2011.09.21 |
달마나 다른 페이지 보여주기 (0) | 2011.09.10 |