源代码(错误的):

<body>
<span id="time"></span>
<script>
functions(){        
var t = new Date();    
var ti = document.getElementById("time").innerHTML;        
ti = t.getHours()+"时"+ t.getMinutes()+"分"+ t.getSeconds()+"秒";              
document.write(ti);          // window.setTimeout(s,1000);    
}    
window.setInterval("s()",1000);    window.onload(s());
</script>
</body>

执行的时候控制台报错:

源代码(修改后):

<body>
<span id="time"></span>
<script>functions(){
var t = new Date();document.getElementById("time").innerHTML=t.getHours()+"时"+ t.getMinutes()+"分"+ t.getSeconds()+"秒";
          // window.setTimeout(s,1000);    
          }    
          window.setInterval("s()",1000);    
          window.onload(s());
</script>
</body>

以上就是如何解决setInterval()定时器只执行一次问题的详细内容,更多请关注ki4网其它相关文章!