$ (Function (){ $ ("# Second"). click (function (){ SendCode ($ ("# second ")); }); V = getCookieValue ("secondsremained"); // obtain the cookie value If (v> 0 ){ Settime ($ ("# second"); // start countdown } }) // Send the verification code Function sendCode (obj ){ Var phonenum = $ ("# phonenum"). val (); Var result = isPhoneNum (); If (result ){ DoPostBack ('$ {base}/login/getcode.htm', backFunc1, {"phonenum": phonenum }); AddCookie ("secondsremained", 60, 60); // Add cookie record, valid for 60 s Settime (obj); // start countdown } } // Use ajax to submit a text message interface to the background. Function doPostBack (url, backFunc, queryParam ){ $. Ajax ({ Async: false, Cache: false, Type: 'post ', Url: url, // the requested action path Data: queryParam, Error: function () {// processing function for request failure }, Success: backFunc }); } Function backFunc1 (data ){ Var d = $. parseJSON (data ); If (! D. success ){ Alert (d. msg ); } Else {// return the verification code Alert ("Simulated Verification Code:" + d. msg ); $ ("# Code"). val (d. msg ); } } // Start countdown Var countdown; Function settime (obj ){ Countdown = getCookieValue ("secondsremained "); If (countdown = 0 ){ Obj. removeAttr ("disabled "); Obj. val ("Get verification code for free "); Return; } Else { Obj. attr ("disabled", true ); Obj. val ("resend (" + countdown + ")"); Countdown --; EditCookie ("secondsremained", countdown, countdown + 1 ); } SetTimeout (function () {settime (obj)}, 1000) // run once every 1000 milliseconds } // Verify that the mobile phone number is valid 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 ('enter a valid mobile phone number! '); Return false; } Else { Return true; } } |