Home > Article > Web Front-end > How to solve the problem that setInterval() timer only executes once
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!