Home > Web Front-end > JS Tutorial > Questions about js counter

Questions about js counter

一个新手
Release: 2017-09-13 10:29:54
Original
1407 people have browsed it


js implementation of counter

Implementation specific code

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
<script type="text/javascript">
  var num=0;  
  function startCount() {
    document.getElementById("count").value=num;
    num=num+1;
      setTimeout("startCount()",1000);     //1处
  }
  setTimeout("startCount()",1000);       //2处
  </script>
  </head>
  <body>
  <form>
  <input type="text" id="count" />
  </form>
  </body>
  </html>
Copy after login

As shown in the code, 1 is obviously an infinite loop, so it is indispensable. The stopwatch counts infinitely. Note here that the unit in js is milliseconds, so it is 1000 milliseconds (1min=60s 1s=1000ms)
But when the code at 2 is deleted, there is only a blank box.

Reason

Netizens understand this. If it is wrong, you can communicate directly
First of all, the program is compiled directly from top to bottom, so the function will be compiled. At this time, the following code That is to say, the HTML code has not been compiled yet, so the function cannot recognize the ID. If the function settimeout is added, it will stay for 1s. At this time, the program has been completed, so it can be passed.
But this statement goes against the process of C language compilation. Because the function is implemented by calling, there is no problem of unrecognizability. . . .

The above is the detailed content of Questions about js counter. 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