Before using the timer, but not a lot of, about the JS timer, generally we can easily think of setinterval and settimeout these two.
Just beginning to learn the JS timer, remember the setinterval, the method is generally used to call the timer in the time of the method once, can be executed in a loop, and settimeout is too long to execute the method in the timer, only execute once . So I take for granted that if you want to loop a method, you have to use setinterval, if it is only called a method once, the use of settimeout, but found that I was wrong, in fact, settimeout can also loop to execute a method. However, you must put the timer in a function and call the function outside so that the timer in the function can be executed in a loop.
For example, I want to implement a click on a button, and then the value in the input box next to the cumulative effect.
Here directly on the code:
<input type= "button" value= "buttons" onclick= "Getval ()" ><input type= "text" id= "num" value= "0" ><script> //Gets the value in the input box val++; document.getElementById (' num '). Value = Val;settimeout (getval,1000) ///One second after calling the Getval method } </script>
This is done by placing the timer in the function and then invoking the function by clicking on the button, thus implementing the timer loop execution.
The use of the settimeout of JS timer