Javascript sets the time interval for two consecutive button clicks. javascript sets the time interval for two consecutive button clicks.

Source: Internet
Author: User

Javascript sets the time interval for two consecutive button clicks. javascript sets the time interval for two consecutive button clicks.

The example in this article describes how to set the interval between two buttons in a row in javascript and share it with you for your reference. The specific implementation method is as follows:

In many cases, we may not want the button contact to be continuously clicked in actual applications. Therefore, we need to set a certain interval before clicking the button again, the following describes how to implement this function through code examples. The Code is as follows:

Copy codeThe Code is as follows: <! DOCTYPE html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Meta name = "author" content = "http://www.bkjia.com/"/>
<Title> helping customers </title>
<Script type = "text/javascript">
Window. onload = function (){
Var odiv = document. getElementById ("thediv ");
Var obt = document. getElementById ("bt ");
Var count = 0;
Var flag = null;
Function done (){
If (count = 0 ){
ClearInterval (flag );
}
Else {
Count = count-1;
}
}
Obt. onclick = function (){
Var val = parseInt (odiv. innerHTML );
If (count = 0 ){
Odiv. innerHTML = val + 1;
Count = 20;
Flag = setInterval (done, 1000 );
}
Else {
Alert ("also needs" + (count) + "seconds to click ");
}
}
}
</Script>
</Head>
<Body>
<Div id = "thediv"> 0 </div>
<Input type = "button" id = "bt" value = "view results"/>
</Body>
</Html>

The above Code meets our requirements and limits the interval of clicking a button. This effect can be extended to other functions, such as limiting the interval of posting, the following describes the implementation process.

The code is annotated as follows:

1. window. onload = function () {}, which specifies that the content of the document is fully 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"), get the button object.
4. var count = 0. Declare a variable and assign the initial value to 0, which is used to store the interval.
5. var flag = null: declare a variable and assign the initial value to null. This variable is used to store the return value of the timer function.
6. function done () {}, which can be continuously called by the timer function to decrease the count.
7. if (count = 0) {clearInterval (flag) ;}, if count = 0, stop the timer function execution.
8. else {count = count-1;}. If it is not equal to 0, delete one.
9. obt. onclick = function () {}, register the event handler for the button.
10. var val = parseInt (odiv. innerHTML), get the content in the div, and convert it to an integer.
11. if (count = 0 ){
Odiv. innerHTML = val + 1;
Count = 20;
Flag = setInterval (done, 1000 );
}
If count is equal to 0, then the content in the div is + 1, the count is set to 20, and the timer function is executed at the same time.
12. else {alert ("also need" + (count) + "seconds to click");}. If count is not equal to zero, how long can it be clicked.

I hope this article will help you design javascript programs.


How does android get the interval of two consecutive button clicks?

Add OnTouchListener to the button to implement the onTouch (View v, MotionEvent event) method, and then use event. getAction () and event. getEventTime () is used to obtain the millisecond values of finger actions and events respectively. The interval between two clicks is the millisecond value difference between two MotionEvent. ACTION_UP actions.

How does javascript obtain the time interval between two keyboard inputs? The two inputs are sent after 1 second

Var oldTime = 0;
Function check (){
Var now = new Date (). getTime ();
Var flag = 0;
If (now-oldTime> 1000)
Flag = 1
OldTime = now;
Return flag;
}

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.