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 ‘30’ 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> |