Verification Code countdown helps implement CountDownButtonHelper and cell phone Verification Code countdown
Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992
When we do the text message verification code function, in order to prevent users from endlessly obtaining the text message verification code, or misoperation, the verification code is messy, we generally add the countdown function on the Button that gets the verification code, thus limiting the interval between users to get the verification code.
This article describes how to use CountDowmTimer to implement the countdown function of the verification code.
First look
CountDownTimer is a class provided by the system for inverted counting. We can set the total countdown time and the countdown interval so that every fixed period of time passes, we can perform our operations in the callback function. The following are the callback methods and constructor of the CountDownTimer class.
CountDownTimer timer = new CountDownTimer(AllTime,Intevel) { @Override public void onTick(long millisUntilFinished) { } @Override public void onFinish() { } };
New CountDownTimer (AllTime, Intevel)
The first parameter is the total time, and the second parameter is the countdown interval.
OnTick () is the callback function that countdown to the interval.
OnFinish () Is the function called after the timer ends.
The following shows my countdown help class.
Package com. example. countdowntimerdemo; import android. OS. countDownTimer; import android. util. log; import android. widget. button;/*** countdown Button help class ** @ author zhaokaiqiang * @ see http://blog.csdn.net/zhaokaiqiang1992 */public class CountDownButtonHelper {// countdown timerprivate CountDownTimer countDownTimer; // The Time-end callback interface private OnFinishListener listener; private Button button;/***** @ param button * indicates the countdown Button * @ param defaultString * default display string * @ param max * requires the maximum countdown value, in seconds * @ param interval * countdown interval, the Unit is seconds */public CountDownButtonHelper (final Button, final String defaultString, int max, int interval) {this. button = button; // because CountDownTimer is not accurate timing, there will be a 1-10 ms error in time when the onTick method is called, which will cause the last second not to call onTick () // Therefore, when the interval is set, 10 ms is subtracted by default, and the error is subtracted. // After the above fine-tuning, the display time of the last second will be accumulated due to a delay of 10 ms, resulting in a display time of max * 10 ms longer than 1 s, and other time display is normal, normal total time countDownTimer = new CountDownTimer (max * 1000, interval * 1000-10) {@ Overridepublic void onTick (long time) {// The first call has an error of 1 to 10 ms, therefore, it takes more than 15 ms to prevent the first number from being displayed, and the second number from displaying 2sbutton. setText (defaultString + "(" + (time + 15)/1000) + "seconds)"); Log. d ("CountDownButtonHelper", "time =" + (time) + "text =" + (time + 15)/1000) ;}@ Overridepublic void OnFinish () {button. setEnabled (true); button. setText (defaultString); if (listener! = Null) {listener. finish () ;}};}/ *** start countdown */public void start () {button. setEnabled (false); countDownTimer. start ();}/*** set the listener for the end of the Countdown ** @ param listener */public void setOnFinishListener (OnFinishListener listener) {this. listener = listener;}/*** time-end callback interface ** @ author zhaokaiqiang **/public interface OnFinishListener {public void finish ();}}
For more information about the test source code, see my github.
Https://github.com/ZhaoKaiQiang/CountDownTimerDemo
For javaScript problems, why do I not execute the countdown after clicking the 'get Verification Code 'button, but double-click the button to execute the code?
T = setTimeout ("timedCount ()", 1000)
Change
T = setInterval ("timedCount ()", 1000)
Function stopCount (){
ClearTimeout (t );
}
Change
Function stopCount (){
ClearInterval (t );
}
JS helps to change the countdown effect of this verification code
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "www.w3.org/..al.dtd">
<Html xmlns = "www.w3.org/5o/xhtml">
<Head>
<Title> </title>
<Style type = "">
. Class1 {
Background-color: green;
}
. Class2 {
Background-color: red;
}
. Class3 {
Background-color: blue;
}
</Style>
<Script src = "jquery-1.7.1.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
/*-------------------------------------------*/
Var InterValObj; // timer variable, control time
Var count = 5; // interval function, executed in 1 second
Var curCount; // number of seconds remaining
Var code = ""; // Verification code
Var codeLength = 6; // verification code length
Function sendMessage (){
$ ("# BtnSendCode"). removeClass ("class1 ");
$ ("# BtnSendCode"). addClass ("class2 ");
CurCount = count;
Var dealType; // Verification Method
Var uid = $ ("# uid"). val (); // user uid
If ($ ("# phone"). attr ("checked") = true ){
DealType = "phone ";
}
Else {
DealT ...... remaining full text>