$ (function () { $ ("#sms"). Click (function () { Sendcode ($ ("#sms")); }); v = getcookievalue ("secondsremained");//Get Cookie value if (V > 0) { SetTime ($ ("#sms"));//Start Countdown } }) Send Verification Code function Sendcode (obj) { var PhoneNumber = $ ("#Phone"). Val (); var result = Isphonenum (PhoneNumber); if (result) { Sending a mobile phone using AJAX to the backend text messaging interface $.post ("/college/code", {Phone:phonenumber}, function (data) { if (data = = "OK") { Alert ("Verification code sent successfully!"); } else { Alert ("The verification code sent failed, please resend!"); } }); Addcookie ("secondsremained", 60, 60);//Add cookie record, effective time 60s SetTime (obj); Start the countdown. } } Start the countdown. var countdown; function SetTime (obj) { Countdown = Getcookievalue ("secondsremained"); if (countdown = = 0) { Obj.removeattr ("Disabled"); Obj.val ("Get 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 (phonenumber) { var Myreg =/^ ((13[0-9]{1}) | ( 15[0-9]{1}) | (18[0-9]{1})) +\D{8}) $/; if (!myreg.test (PhoneNumber)) { Alert (' Please enter a valid mobile number! ‘); return false; } else { return true; } } 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 } } } |