Home > Web Front-end > JS Tutorial > body text

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

一个新手
Release: 2017-09-12 09:41:53
Original
4547 people have browsed it

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>
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template