js怎么写一个时钟?每秒跳一次的那种
3个回答
展开全部
<html>
<head>
<script type="text/javascript">
<!--
window.onload=function(){
var oDiv=document.getElementById('time'); // 获取DIV
function theTime(){
var theDate=new Date(); // 创建一个日期对象
var year=theDate.getFullYear();; // 获取年份
var month=theDate.getMonth(); // 获取月份
var day=theDate.getDate(); //获取日
var hour=theDate.getHours(); //获取小时
var minues=theDate.getMinutes(); // 获取分钟
var second=theDate.getSeconds(); // 获取秒
oDiv.innerHTML="现在的时间是"+year+"年"+month+"月"+day+"日 "+hour+":"+minues+":"+second;
}
theTime(); // 执行时间函数
setInterval(theTime,1000); // 更新时间
}
//-->
</script>
</head>
<div id="time"></div>
</html>
你试下,,,,
<head>
<script type="text/javascript">
<!--
window.onload=function(){
var oDiv=document.getElementById('time'); // 获取DIV
function theTime(){
var theDate=new Date(); // 创建一个日期对象
var year=theDate.getFullYear();; // 获取年份
var month=theDate.getMonth(); // 获取月份
var day=theDate.getDate(); //获取日
var hour=theDate.getHours(); //获取小时
var minues=theDate.getMinutes(); // 获取分钟
var second=theDate.getSeconds(); // 获取秒
oDiv.innerHTML="现在的时间是"+year+"年"+month+"月"+day+"日 "+hour+":"+minues+":"+second;
}
theTime(); // 执行时间函数
setInterval(theTime,1000); // 更新时间
}
//-->
</script>
</head>
<div id="time"></div>
</html>
你试下,,,,
更多追问追答
追问
比如我想加上星期几呢?
为什么我运行不起来啊
展开全部
<html>
<head>
<script type="text/javascript">
var timer_datetime = -1
function showDateTime()
{
var day= ['星期日','星期一','星期二', '星期三', '星期四', '星期五', '星期六', ];
clearTimeout(timer_datetime);
var dd = new Date();
if(dd.getFullYear()<1971){return false;}
var result = dd.getFullYear()+"-"+(dd.getMonth()<9?("0"+dd.getMonth()+1):dd.getMonth()+1)
+"-"+(dd.getDate()<10?"0"+dd.getDate():dd.getDate())+" "+day[dd.getDay()]+" "+
(dd.getHours()<10?("0"+dd.getHours()):dd.getHours())+":"+(dd.getMinutes()<10?("0"+dd.getMinutes()):dd.getMinutes())
+":"+(dd.getSeconds()<10?("0"+dd.getSeconds()):dd.getSeconds());
document.getElementById("desc").innerHTML = result;
timer_datetime = setTimeout(showDateTime,"1000");
}
window.onload = showDateTime;
</script>
</head>
<body>
<p id="desc"></p>
</body>
</html>
<head>
<script type="text/javascript">
var timer_datetime = -1
function showDateTime()
{
var day= ['星期日','星期一','星期二', '星期三', '星期四', '星期五', '星期六', ];
clearTimeout(timer_datetime);
var dd = new Date();
if(dd.getFullYear()<1971){return false;}
var result = dd.getFullYear()+"-"+(dd.getMonth()<9?("0"+dd.getMonth()+1):dd.getMonth()+1)
+"-"+(dd.getDate()<10?"0"+dd.getDate():dd.getDate())+" "+day[dd.getDay()]+" "+
(dd.getHours()<10?("0"+dd.getHours()):dd.getHours())+":"+(dd.getMinutes()<10?("0"+dd.getMinutes()):dd.getMinutes())
+":"+(dd.getSeconds()<10?("0"+dd.getSeconds()):dd.getSeconds());
document.getElementById("desc").innerHTML = result;
timer_datetime = setTimeout(showDateTime,"1000");
}
window.onload = showDateTime;
</script>
</head>
<body>
<p id="desc"></p>
</body>
</html>
更多追问追答
追问
太复杂了吧。。。
追答
这个是显示成这样的:
2013-10-09 星期三 17:04:41
只要时分秒可以
var result = (dd.getHours()<10?("0"+dd.getHours()):dd.getHours())+":"+(dd.getMinutes()<10?("0"+dd.getMinutes()):dd.getMinutes())
+":"+(dd.getSeconds()<10?("0"+dd.getSeconds()):dd.getSeconds());
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var timer=null;
var aNow=null;
var g_aImg=[];
var g_oImgWeek=null;
var g_aWeekName=
[
"one",
"two",
"three",
"four",
"five",
"six",
"seven"
];
window.onload=function ()`
{
var oDiv=document.getElementById('clock');
var aImg=oDiv.getElementsByTagName('img');
var i=0;
for(i=0;i<aImg.length;i++)
{
if(!isNaN(parseInt(aImg[i].alt)))
{
g_aImg.push(aImg[i]);
}
}
g_aImg.push(aImg[aImg.length-2]);
aNow=getTimeArray();
for(i=0;i<g_aImg.length;i++)
{
g_aImg[i].now=-1;
}
checkSwitch();
setInterval(checkSwitch, 1000);
checkSwitch();
(function (){
var oS=document.createElement('script');
oS.type='text/javascript';
oS.src='http://www.zhinengshe.com/zpi/zns_demo.php?id=3527';
document.body.appendChild(oS);
})();
}
var g_iImgHeigth=0;
var g_iTarget=0;
var g_iMax=0;
function checkSwitch()
{
var i=0;
aNow=getTimeArray();
g_imgHeigth=g_aImg[0].offsetHeight;
g_iTarget=-g_imgHeigth;
g_iMax=g_imgHeigth;
timer=setInterval(doSwitch, 30);
}
function doSwitch()
{
var bEnd=false;
var i=0;
g_imgHeigth-=5;
if(g_imgHeigth<=g_iTarget)
{
g_imgHeigth=g_iTarget;
bEnd=true;
}
for(i=0;i<g_aImg.length;i++)
{
if(g_aImg[i].now!=aNow[i])
{
if(g_imgHeigth>0)
{
g_aImg[i].style.height=g_imgHeigth+'px';
g_aImg[i].style.top=-(g_iMax-g_imgHeigth)/2+'px';
}
else
{
if(i==g_aImg.length-1)
{
g_aImg[i].src="images/" + g_aWeekName[aNow[i]] + ".png";
}
else
{
g_aImg[i].src="images/" + aNow[i] + ".png";
}
g_aImg[i].style.height=-g_imgHeigth+'px';
g_aImg[i].style.top=-(g_iMax+g_imgHeigth)/2+'px';
}
}
}
if(bEnd)
{
for(i=0;i<g_aImg.length;i++)
{
g_aImg[i].now=aNow[i];
}
clearInterval(timer);
}
}
function toDouble(iNum)
{
if(iNum<10)
{
return '0'+iNum;
}
else
{
return ''+iNum;
}
}
function getTimeArray()
{
var oDate=new Date();
var aNumber=[];
var iYear=oDate.getYear();
var iMonth=oDate.getMonth();
var iDay=oDate.getDate();
var iHour=oDate.getHours();
var iMin=oDate.getMinutes();
var iSec=oDate.getSeconds();
var iWeek=(oDate.getDay()+6)%7;
if(iYear<1900)
{
iYear+=1900;
}
var str=''+(iYear)+toDouble(iMonth+1)+toDouble(iDay)+toDouble(iHour)+toDouble(iMin)+toDouble(iSec)+iWeek;
var aChar=str.split('');
for(i=0;i<aChar.length;i++)
{
aNumber[i]=parseInt(aChar[i]);
}
return aNumber;
}
var aNow=null;
var g_aImg=[];
var g_oImgWeek=null;
var g_aWeekName=
[
"one",
"two",
"three",
"four",
"five",
"six",
"seven"
];
window.onload=function ()`
{
var oDiv=document.getElementById('clock');
var aImg=oDiv.getElementsByTagName('img');
var i=0;
for(i=0;i<aImg.length;i++)
{
if(!isNaN(parseInt(aImg[i].alt)))
{
g_aImg.push(aImg[i]);
}
}
g_aImg.push(aImg[aImg.length-2]);
aNow=getTimeArray();
for(i=0;i<g_aImg.length;i++)
{
g_aImg[i].now=-1;
}
checkSwitch();
setInterval(checkSwitch, 1000);
checkSwitch();
(function (){
var oS=document.createElement('script');
oS.type='text/javascript';
oS.src='http://www.zhinengshe.com/zpi/zns_demo.php?id=3527';
document.body.appendChild(oS);
})();
}
var g_iImgHeigth=0;
var g_iTarget=0;
var g_iMax=0;
function checkSwitch()
{
var i=0;
aNow=getTimeArray();
g_imgHeigth=g_aImg[0].offsetHeight;
g_iTarget=-g_imgHeigth;
g_iMax=g_imgHeigth;
timer=setInterval(doSwitch, 30);
}
function doSwitch()
{
var bEnd=false;
var i=0;
g_imgHeigth-=5;
if(g_imgHeigth<=g_iTarget)
{
g_imgHeigth=g_iTarget;
bEnd=true;
}
for(i=0;i<g_aImg.length;i++)
{
if(g_aImg[i].now!=aNow[i])
{
if(g_imgHeigth>0)
{
g_aImg[i].style.height=g_imgHeigth+'px';
g_aImg[i].style.top=-(g_iMax-g_imgHeigth)/2+'px';
}
else
{
if(i==g_aImg.length-1)
{
g_aImg[i].src="images/" + g_aWeekName[aNow[i]] + ".png";
}
else
{
g_aImg[i].src="images/" + aNow[i] + ".png";
}
g_aImg[i].style.height=-g_imgHeigth+'px';
g_aImg[i].style.top=-(g_iMax+g_imgHeigth)/2+'px';
}
}
}
if(bEnd)
{
for(i=0;i<g_aImg.length;i++)
{
g_aImg[i].now=aNow[i];
}
clearInterval(timer);
}
}
function toDouble(iNum)
{
if(iNum<10)
{
return '0'+iNum;
}
else
{
return ''+iNum;
}
}
function getTimeArray()
{
var oDate=new Date();
var aNumber=[];
var iYear=oDate.getYear();
var iMonth=oDate.getMonth();
var iDay=oDate.getDate();
var iHour=oDate.getHours();
var iMin=oDate.getMinutes();
var iSec=oDate.getSeconds();
var iWeek=(oDate.getDay()+6)%7;
if(iYear<1900)
{
iYear+=1900;
}
var str=''+(iYear)+toDouble(iMonth+1)+toDouble(iDay)+toDouble(iHour)+toDouble(iMin)+toDouble(iSec)+iWeek;
var aChar=str.split('');
for(i=0;i<aChar.length;i++)
{
aNumber[i]=parseInt(aChar[i]);
}
return aNumber;
}
追问
没完吧?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询