The use of the settimeout of JS timer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.