html setTimeout无法实现时间刷新

[复制链接]
查看11 | 回复1 | 2011-2-24 03:19:58 | 显示全部楼层 |阅读模式
我写了一个html页面,显示当前时间并美妙刷新。可现在时间能显示,但一直停留在当前时间,无法实现美妙自动刷新,请问是哪里出了问题??
程序代码如下。。

!DOCTYPE html PUBLIC \\\"-//W3C//DTD XHTML 1.0 Transitional//EN\\\" \\\"<a href=\\\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\\\" target=\\\"_blank\\\">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>\\\"
html xmlns=\\\"<a href=\\\"http://www.w3.org/1999/xhtml\\\" target=\\\"_blank\\\">http://www.w3.org/1999/xhtml</a>\\\"
head
meta http-equiv=\\\"Content-Type\\\" content=\\\"text/html; charset=utf-8\\\" /
titleUntitled Document/title

script language=\\\"JavaScript\\\" type=\\\"text/javascript\\\"



function showtime(){

today=new Date();
// 得到浏览器当前的时间,是浏览者本机的时间。此时间中包括所有具体的时间信息。

hour=today.getHours()
minu=today.getMinutes()
sec=today.getSeconds()



if(hour6)hello=\\\'凌晨好\\\'

else if(hour9)hello=\\\'早上好\\\'

else if(hour12)hello=\\\'上午好\\\'

else if(hour14)hello=\\\'中午好\\\'

else if(hour17)hello=\\\'下午好\\\'

else if(hour19)hello=\\\'傍晚好\\\'

else if(hour22)hello=\\\'晚上好\\\'

else {hello=\\\'夜里好\\\'}




now=\\\', 现在是\\\' hour \\\'点\\\' minu \\\'分\\\' sec \\\'秒\\\';


document.write(\\\"font color=red\\\" hello \\\"/font\\\"); // 显示当前时间段的欢迎词,\\\"font color=red\\\"给 hello 提示信息加上红颜色,/font加颜色结束处。

document.write(now);
window.setTimeout(\\\"showtime()\\\",1000);
//window.setInterval(\\\"showtime()\\\",1000);

}

/script
/head

body

script
showtime()

/script

/body
/html
回复

使用道具 举报

千问 | 2011-2-24 03:19:58 | 显示全部楼层
<pre id=\\\"best-answer-content\\\" class=\\\"reply-text mb10\\\">setTimeout() 只执行 code 一次。如果要多次调用,请使用 setInterval() 或者让 code 自身再次调用 setTimeout()。 你在body中使用的是javascript 而不是让他在页面中输出,你在javascript中输出时不行的,要在页面输出才可以。你的代码稍微改一下就可以了

!DOCTYPE html PUBLIC \\\"-//W3C//DTD XHTML 1.0 Transitional//EN\\\" \\\"<a href=\\\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\\\" target=\\\"_blank\\\">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>\\\"
html xmlns=\\\"<a href=\\\"http://www.w3.org/1999/xhtml\\\" target=\\\"_blank\\\">http://www.w3.org/1999/xhtml</a>\\\"
head
meta http-equiv=\\\"Content-Type\\\" content=\\\"text/html; charset=utf-8\\\" /
title时间/title

script language=\\\"JavaScript\\\" type=\\\"text/javascript\\\"!--



function showtime(){

today=new Date();
// 得到浏览器当前的时间,是浏览者本机的时间。此时间中包括所有具体的时间信息。

hour=today.getHours()
minu=today.getMinutes()
sec=today.getSeconds()

if(hour6)hello=\\\'凌晨好\\\'

else if(hour9)hello=\\\'早上好\\\'

else if(hour12)hello=\\\'上午好\\\'

else if(hour14)hello=\\\'中午好\\\'

else if(hour17)hello=\\\'下午好\\\'

else if(hour19)hello=\\\'傍晚好\\\'

else if(hour22)hello=\\\'晚上好\\\'

else {hello=\\\'夜里好\\\'}

document.myform.date.value=(hello\\\",现在是\\\" hour \\\"点\\\" minu 分\\\' sec \\\'秒\\\');

window.setTimeout(\\\"showtime()\\\",1000);


}

/script
/head

body onload=\\\" showtime()\\\"
form name=\\\"myform\\\"
input type=\\\"text\\\" name=\\\"date\\\" value=\\\"\\\" size=\\\"30\\\" style=\\\"border-style:none; background-color:#FFFFFF\\\" /
/form
/body
/html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行