JavaScript sets the time interval of two consecutive click buttons

Source: Internet
Author: User
Tags time interval

Many times we in the actual application, may not want the button contact is uninterrupted clicks, therefore must limit certain time interval only then to be able to click the button again, the following is through the code example to introduce how to implement this function, the code is as follows:

The code is as follows:

0

The above code to achieve our requirements, you can limit the time to click the button, this effect can be extended to other functions, such as limit the time interval of posting, and so on, the following describes its implementation process.

The code comments are as follows:

1.window.onload=function () {}, which stipulates that the contents of the document are completely loaded before executing the code in the function.

2.var Odiv=document.getelementbyid ("Thediv") to get the div element object.

3.var Obt=document.getelementbyid ("BT") to get the button object.

4.var count=0, declares a variable and assigns an initial value of 0, which is used to store the interval time.

5.var Flag=null, declare a variable and assign an initial value to NULL, which is used to store the return value of the timer function.

6.function done () {}, this function can be called repeatedly by the timer function to decrement count.

7.if (count==0) {clearinterval (flag);}, if count==0, stop the execution of the timer function.

8.else{count=count-1}, if not equal to 0, then subtract one action.

9.obt.onclick=function () {}, click event handler for button registration.

10.var Val=parseint (odiv.innerhtml), gets the contents of the DIV and converts it to an integer.

11.if (count==0) {

odiv.innerhtml=val+1;

count=20;

Flag=setinterval (done,1000);

}

If Count equals 0, then the content in the Div is +1, and count is set to 20, while the boot timer function executes.

12.else{alert ("also need" + (count) + seconds to click); if count is not equal to zero, then how long does it take to eject?

Related Article

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.