setInterval和setTimeOut

bjxjzh 2006-11-11 12:05:44
区别是后者只执行一次

为什么时钟的js里面 用后者 效果一样呢?

<script>
function tick() {
var hours, minutes, seconds, xfile;
var intHours, intMinutes, intSeconds;
var today;
today = new Date();
intHours = today.getHours();
intMinutes = today.getMinutes();
intSeconds = today.getSeconds();
if (intHours == 0) {
hours = "12:";
xfile = "午夜";
} else if (intHours < 12) {
hours = intHours+":";
xfile = "上午";
} else if (intHours == 12) {
hours = "12:";
xfile = "正午";
} else {
intHours = intHours - 12
hours = intHours + ":";
xfile = "下午";
}
if (intMinutes < 10) {
minutes = "0"+intMinutes+":";
} else {
minutes = intMinutes+":";
}
if (intSeconds < 10) {
seconds = "0"+intSeconds+" ";
} else {
seconds = intSeconds+" ";
}
timeString = xfile+hours+minutes+seconds;
document.getElementById("t").innerHTML=timeString
window.setTimeout("tick();", 1000);
}
window.onload=tick
</script>
<div id="t"></div>
...全文
115 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bjxjzh 2006-11-11
  • 打赏
  • 举报
回复
真是。。。看来我连读这么简单的代码都不会读呢~~
懒牛科技 2006-11-11
  • 打赏
  • 举报
回复
setInterval和setTimeOut 就象你说的,后者是执行一次,
但是在对于时钟的js里面,window.setTimeout("tick();", 1000);是包括在程序的内部,这样每一次调用程序,则又一次执行window.setTimeout("tick();", 1000);而它又调用了程序,所以效果一样,
就想递归一样!

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧