Home  >  Article  >  Web Front-end  >  How to solve the problem that setInterval() timer only executes once

How to solve the problem that setInterval() timer only executes once

一个新手
一个新手Original
2017-09-12 09:41:534494browse

Source code (wrong):

<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>

The console reports an error when executing:

Source code (after modification):

<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>

The above is the detailed content of How to solve the problem that setInterval() timer only executes once. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn