javascript, 网页如何显一个动态时间的 秒数 。代码是如怎么写....

JavaScript017

javascript, 网页如何显一个动态时间的 秒数 。代码是如怎么写....,第1张

把下面的代码保存成HTML文件

<body>

<span id="time"></span>

</body>

<script type="text/javascript">

function showtime(){

var str,time

time=new Date()

str="欢迎光临,现在是"+time.getFullYear()+"年"+(time.getMonth()+1)+"月"+time.getDate()+"日 "+time.getHours()+":"+time.getMinutes()+":"+time.getSeconds()

document.getElementById("time").innerHTML=str

}

setInterval("showtime()",1000)

</script>

<html>

<head>

<title>时钟特效</title>

<script type="text/javascript">

function disptime(){

var today = new Date(); //获得当前时间

var hh = today.getHours(); //获得小时、分钟、秒

var mm = today.getMinutes();

var ss = today.getSeconds();

/*设置div的内容为当前时间*/

document.getElementById("myclock").innerHTML="<h1>现在是:"+hh+":"+mm+":"+ss+"<h1>";

/*

使用setTimeout在函数disptime()体内再次调用setTimeout

设置定时器每隔1秒(1000毫秒),调用函数disptime()执行,刷新时钟显示

*/

var myTime=setTimeout("disptime()",1000);

}

</script>

</head>

<body onload="disptime()">

<div id="myclock"></div>

</body>。

<html>

<head><title>test</title></head>

<body>

<input id="hour" style="width:2em" value="5" />:<input id="min" style="width:2em" value="20" />

<button id="do">确定</button>

<div id="info" style="display:none">

当前时间:<span id="cur_time"></span><br>

时 间 差:<span id="time_span"></span>

</div>

<script type="text/javascript">

function $(id){ return document.getElementById(id)}

function time_add_sec(secs){

if(secs != 0){ secs = 1}

window.curtime += secs

var span = Math.abs(curtime - starttime)

var cur_time = []

var time_span = []

cur_time[0] = Math.floor(curtime / 3600)

cur_time[1] = Math.floor((curtime % 3600) / 60)

cur_time[2] = Math.floor(curtime % 60)

time_span[0] = Math.floor(span / 3600)

time_span[1] = Math.floor((span % 3600) / 60)

time_span[2] = Math.floor(span % 60)

$("cur_time").innerHTML = cur_time.join(":")

$("time_span").innerHTML = time_span.join(":")

}

function time_span(){

var now = new Date()

var cur_time = []

cur_time[0] = now.getHours()

cur_time[1] = now.getMinutes()

cur_time[2] = now.getSeconds()

window.curtime = cur_time[0]*3600+cur_time[1]*60+cur_time[2]

window.starttime = $("hour").value*3600+$("min").value*60

time_add_sec(0)

$("info").style.display = "block"

setInterval(time_add_sec,1000)

}

window.onload=function (){ $("do").onclick = time_span}

</script>

</body>

</html>

这个算法占用系统资源相当少了,跳动应该是你电脑卡吧,我的电脑一点问题都没有。