<script type="text/javascript"> onload=function(){ start(); clock = window.setInterval(‘start()‘,1000); btn = document.getElementById(‘btn‘); btn.addEventListener(‘click‘,timeOut); btnTxt = btn.childNodes[0]; } function start(){ var date = new Date(); var s = date.getSeconds(); var i = date.getMinutes(); var h = date.getHours(); var d = date.getDate(); var m = date.getMonth()+1; var y = date.getFullYear(); //date.toLocaleDateString()——格式化本地日期 //date.toLocaletimeString()——格式化本地时间 var week = [‘星期天‘,‘星期一‘,‘星期二‘,‘星期三‘,‘星期四‘,‘星期五‘,‘星期六‘]; week = week[date.getDay()]; var str = y+‘-‘+m+‘-‘+d+‘ ‘+h+‘:‘+i+‘:‘+s+‘ ‘+week; var dv = document.getElementById(‘dv‘); var txt = document.createTextNode(str); dv.replaceChild(txt,dv.childNodes[0]); } function timeOut(){ var start = document.createTextNode(‘开始‘); if(btn.firstChild.nodeValue==‘暂停‘){ clearInterval(clock); var start = document.createTextNode(‘开始‘); btn.replaceChild(start,btn.childNodes[0]); }else if(btn.firstChild.nodeValue==‘开始‘){ clock = window.setInterval(‘start()‘,1000); var start = document.createTextNode(‘暂停‘); btn.replaceChild(start,btn.childNodes[0]); } } </script>
<div id="dv">这里显示时钟</div>
<button id="btn">暂停</button>
js时钟显示
时间: 2024-10-10 20:37:30