This section is a bit interested in js. It is interesting to have a few small js experiments. One of them is to control the display of the clock. If you are interested, you can study it! Let's take a look at the following code:
First, declare two variables in the js Code: timerID and timerRunning. You can see that in the showTimes method, assign timerId: timerID = setTimeout ("showTimes ()", 1000 ); indicates that the Browser executes the showTimes method 1000 milliseconds after receiving the setTimeout command. to cancel the timeout setting, it calls stopClock (). In this method, there is a clearTimeout (timerID ); you can cancel the time-out setting. If the time-out setting has not occurred, the showTimes method will not be called. The setTimeout () method is very interesting, if you are interested, you can compare setInterval ("showTime ()", 1000) to see the execution results of the two methods. As the name suggests, timerRunning indicates whether the time is displayed, it mainly serves stopClock () and showTimes.
In addition, we can see that the showTimes method in js contains a document. clock. face. value = timerVal; document contains the entire html document. In html, you can see that the name attribute of form is clock, and the input element of form contains text of type = "text, its name attribute is face, so that you can find the text in the html page and set its value to the time we get. This will display the time in the corresponding text box on the page, to obtain the time, you can declare a time object var now = new Date (); then call its getYear () and getMonth () to obtain the time. After setting, call this method during html loading, that is, <body onLoad = "startClock ()"> in this way, the startClock method is automatically called when the html page is loaded, so that the time is displayed in the page text.
Js is very interesting. If you are interested, you can study it together!
This article is from the "HDDevTeam" blog