更新Time Difference代码

之前写过一篇<[JavaScript]Time Difference>,原来的代码非常乱七八糟,基本每个人的时间都是各自计算然后显示的.

源代码如下:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<title>What Time Is It There?</title>
<script type=”text/javascript”>
function startTime()
{
var today = new Date();
var GMT_h = today.getHours() + ((new Date()).getTimezoneOffset())/60;
var kem_h = GMT_h + 8;
var kem_m = today.getMinutes()
var kem_s = today.getSeconds()
var georgemiller_h = GMT_h + 2
var georgemiller_m = today.getMinutes()
var georgemiller_s = today.getSeconds()
var max_h = GMT_h – 5
var max_m = today.getMinutes()
var max_s = today.getSeconds()
var gavinzhang_h = GMT_h – 4
var gavinzhang_m = today.getMinutes()
var gavinzhang_s = today.getSeconds()
kem_h = checkHour(kem_h)
kem_m = checkTime(kem_m)
kem_s = checkTime(kem_s)
georgemiller_h = checkHour(georgemiller_h)
georgemiller_m = checkTime(georgemiller_m)
georgemiller_s = checkTime(georgemiller_s)
max_h = checkHour(max_h)
max_m = checkTime(max_m)
max_s = checkTime(max_s)
gavinzhang_h = checkHour(gavinzhang_h)
gavinzhang_m = checkTime(gavinzhang_m)
gavinzhang_s = checkTime(gavinzhang_s)
document.getElementById(‘kem’).innerHTML=kem_h+”:”+kem_m+”:”+kem_s
document.getElementById(‘georgemiller’).innerHTML=+georgemiller_h+”:”+georgemiller_m+”:”+georgemiller_s
document.getElementById(‘max’).innerHTML=max_h+”:”+max_m+”:”+max_s
document.getElementById(‘gavinzhang’).innerHTML=gavinzhang_h+”:”+gavinzhang_m+”:”+gavinzhang_s
t=setTimeout(‘startTime()’,500)
}

function checkTime(i)
{
if (i<10)
{i=”0″ + i}
return i
}

function checkHour(i)
{
if (i<0)
{i= i + 24}
return i
}

</script>
</head>

<body onLoad=”startTime()”>
<p><a href=”http://akem.name”>Kem</a> @ Shanghai,China:</p>
<p><div id=”kem”></div></p>
<p><a href=”http://www.renren.com/profile.do?id=249522058″>Georgemiller</a> @ Nancy,France:</p>
<p><div id=”georgemiller”></div></p>
<p><a href=”http://weibo.com/1249208195″>Max</a>@Wisconsin,USA:</p>
<p><div id=”max”></div></p>
<p><a href=”http://weibo.com/2118476123″>Gavinzhang</a> @ Charlottesville,Virginia,USA:</p>
<p><div id=”gavinzhang”></div></p>
</body>
</html>

 

 

 

这几天,尝试精简了代码,提取了一些公共函数,是代码量减少了一大半.

新代码如下:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=GB2312″ />
<title>What Time Is It There?</title>
</head>

<body>
<p><a href=”http://akem.name”>Kem</a> @ Shanghai,China:</p>
<p><div id=”kem”></div></p>
<p><a href=”http://www.renren.com/profile.do?id=249522058″>Georgemiller</a> @ Nancy,France:</p>
<p><div id=”georgemiller”></div></p>
<p><a href=”http://weibo.com/2118476123″>Gavinzhang</a> @ Charlottesville,Virginia,USA:</p>
<p><div id=”gavinzhang”></div></p>

<script type=”text/javascript”>
function main()
{
startTime(8,’kem’)
startTime(2,’georgemiller’)
startTime(4,’gavinzhang’)
t=setTimeout(‘main()’,500)
}

function startTime(sq,name)
{
var today = new Date();
var GMT_h = today.getHours() + ((new Date()).getTimezoneOffset())/60;
var h = GMT_h + sq;
var m = today.getMinutes();
var s = today.getSeconds();
h = checkHour(h);
m = checkTime(m);
s = checkTime(s);
document.getElementById(name).innerHTML=h+”:”+m+”:”+s;
}

function checkTime(i)
{
if (i<10)
{i=”0″ + i;}
return i;
}

function checkHour(i)
{
if (i<0)
{i= i + 24;}
return i;
}
main()
</script>
</body>
</html>

 效果演示

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注