JavaScript click on Button Countdown example

Source: Internet
Author: User

HTML code:

The code is as follows Copy Code

<input type= "button" id= "btn" value= "Free access to authentication code"/>

JS Code:

The code is as follows Copy Code
<script type= "Text/javascript" >
var wait=60;
function Time (o) {
if (wait = = 0) {
O.removeattribute ("Disabled");
O.value= "Free access to verification code";
wait = 60;
} else {
O.setattribute ("Disabled", true);
O.value= "Resend (" + Wait + ")";
wait--;
settimeout (function () {
Time (O)
},
1000)
}
}
document.getElementById ("Btn"). Onclick=function () {time (this);}
</script>

The effect chart is as follows

Example

The code is as follows Copy Code

/**
* Countdown function
* Need to bind the button to the Click event
* such as: <input contenteditable=false value= send sms Type=button data-cke-pa-onclick= "setinterval (' CountDown"), 1000); "data-cke-editable=" 1 ">
* 30 represents the number of seconds, the number of seconds to be counted can be changed
*/
function Countdown (Obj,second) {
If the number of seconds is still greater than 0, it means the countdown is not over yet.
if (second>=0) {
Get the text on the default button
if (typeof buttondefaultvalue = = ' undefined ') {
Buttondefaultvalue = Obj.defaultvalue;
}
button to non-clickable state
Obj.disabled = true;
The contents of the button render the countdown status
Obj.value = Buttondefaultvalue+ ' (' +second+ ') ';
Time Minus One
second--;
Repeat execution after one second
settimeout (function () {countdown (Obj,second);},1000);
Otherwise, the button resets to the initial state
}else{
button not to click state
obj.disabled = false;
The content in the button is restored to its original state
Obj.value = Buttondefaultvalue;
}
}

HTML code

The code is as follows Copy Code
JS button 30 seconds Countdown effect <p>
<input onclick= "Countdown (this,30);" type= "button" value= "Send SMS"/></p>
June-Tai Blog www.111cn.net<p>

In the button-bound JS Click event &lsquo;30&rsquo; represents the countdown seconds, you can set the number of seconds in the binding, flexible application. Of course, this countdown is just a front-end control, if the need for a more secure approach should be the server side also make judgments. </p>

Example, this example is based on jquery.

  code is as follows copy code

<title>test count down button</title>
<script src= "/js/jquery.js" Text/javascript "></script>
<script type=" Text/javascript ">
$ (function () {
$ (' #btn '). Click (function () {
var count = 3;
var countdown = SetInterval (countdown, 1000);

Function countdown () {
$ ("#btn"). attr ("Disabled", true);
$ ("#btn"). Val ("Please wait" + count + "seconds!");
if (count = = 0) {
$ ("#btn"). Val ("Submit"). Removeattr ("Disabled");
Clearinterval (Countdown);
}
count--;
}
})
});
</script>
<body>
<input type= "button" id= "btn" value= "Submit"/>
</body>

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.