JS implementation after sending SMS verification code Countdown function (Ignore page refresh)

Source: Internet
Author: User

[1]. [Code] This is the Send verification Code button on the page jumps to [1] [2] [3]
<input id= "Second" type= "button" value= "Get Verification Code free"/>
[2]. [Code] JS operation of the cookie jumps to [1] [2] [3]

Add a cookie when sending a verification code
function Addcookie (name,value,expireshours) {
var cookiestring=name+ "=" +escape (value);
Determine if the expiration time is set, 0 is invalid when the browser is closed
if (expireshours>0) {
var date=new date ();
Date.settime (Date.gettime () +expireshours*1000);
cookiestring=cookiestring+ "; expires=" + date.toutcstring ();
}
document.cookie=cookiestring;
}
Modify the value of a cookie
function Editcookie (name,value,expireshours) {
var cookiestring=name+ "=" +escape (value);
if (expireshours>0) {
var date=new date ();
Date.settime (Date.gettime () +expireshours*1000); Unit is milliseconds
cookiestring=cookiestring+ "; expires=" + date.togmtstring ();
}
document.cookie=cookiestring;
}
Get the value of a cookie by name
function Getcookievalue (name) {
var Strcookie=document.cookie;
var arrcookie=strcookie.split (";");
for (Var i=0;i<arrcookie.length;i++) {
var arr=arrcookie[i].split ("=");
if (arr[0]==name) {
Return unescape (arr[1]);
Break
}else{
Return "";
Break
}
}

}
[3]. [Code] main logic code jumps to [1] [2] [3]

$ (function () {
$ ("#second"). Click (function () {
Sendcode ($ ("#second"));
});
v = getcookievalue ("secondsremained");//Get Cookie value
if (v>0) {
SetTime ($ ("#second"));//Start Countdown
}
})
Send Verification Code
function Sendcode (obj) {
var phonenum = $ ("#phonenum"). Val ();
var result = Isphonenum ();
if (result) {
Dopostback (' ${base}/login/getcode.htm ', backfunc1,{"Phonenum":p Honenum});
Addcookie ("secondsremained", 60,60);//Add cookie record, effective time 60s
SetTime (obj);//Start Countdown
}
}
Sending a mobile phone using AJAX to the backend text messaging interface
function Dopostback (url,backfunc,queryparam) {
$.ajax ({
Async:false,
Cache:false,
Type: ' POST ',
url:url,//the requested action path
Data:queryparam,
Error:function () {//Request failed handler function
},
Success:backfunc
});
}
function BackFunc1 (data) {
var d = $.parsejson (data);
if (!d.success) {
alert (d.msg);
}else{//Return Verification Code
Alert ("Analog Verification Code:" +d.msg);
$ ("#code"). Val (d.msg);
}
}
Start the countdown.
var countdown;
function SetTime (obj) {
Countdown=getcookievalue ("secondsremained");
if (countdown = = 0) {
Obj.removeattr ("Disabled");
Obj.val ("Free access to Verification code");
Return
} else {
Obj.attr ("Disabled", true);
Obj.val ("Resend (" + Countdown + ")");
countdown--;
Editcookie ("secondsremained", countdown,countdown+1);
}
SetTimeout (function () {settime (obj)},1000)//every 1000 milliseconds
}
Verifying the phone number is legal
function Isphonenum () {
var phonenum = $ ("#phonenum"). Val ();
var Myreg =/^ ((13[0-9]{1}) | ( 15[0-9]{1}) | (18[0-9]{1})) +\D{8}) $/;
if (!myreg.test (Phonenum)) {
Alert (' Please enter a valid mobile number! ‘);
return false;
}else{
return true;
}
}

JS implementation after sending SMS verification code Countdown function (Ignore page refresh)

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.