Related readings:
Based on jQuery, the countdown function after sending SMS verification code is realized (neglect the page and close it)
The following code is the code that the editor brings to you after sending a SMS verification code to you. The code is simple and easy to understand.
The specific code is as follows:
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="Generator" content="EditPlus®"><meta name="Author" content=""><meta name="Keywords" content=""><meta name="Description" content=""><title>Document</title><script src="http://cdn.bootcss.com/jquery/3.1.0/jquery.js"></script><script src="http://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.js"></script><!-- <script src="//cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>--></head><body><input id="phonenum" type="text" value="18518181818"/><input id="second" type="button" value="Free to get verification code" /></body><script>//Add cookiefunction when sending verification code addCookie(name,value,expiresHours){ //Judge whether to set the expiration time, 0 means invalid when closing the browser if(expiresHours>0){ var date=new Date(); date.setTime(date.getTime()+expiresHours*1000); $.cookie(name, escape(value), {expires: date});}else{$.cookie(name, escape(value));}} //Modify the value of the cookie function editCookie(name,value,expiresHours){ if(expiresHours>0){ var date=new Date(); date.setTime(date.getTime()+expiresHours*1000); //The unit is milliseconds$.cookie(name, escape(value), {expires: date});} else{$.cookie(name, escape(value));}} //Get the value of the cookie based on the name function getCookieValue(name){ return $.cookie(name);}$(function(){$("#second").click(function (){sendCode($("#second"));});v = getCookieValue("secondsremained");//Get the 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":phonenum});addCookie("secondsremained",60,60);//Add cookie record, valid time 60 settime(obj);//Start countdown}}//Start countdown var countdown; function settime(obj) { countdown=getCookieValue("secondsremained"); if (countdown == 0) { obj.removeAttr("disabled"); obj.val("free obtain verification code"); return;} else { obj.attr("disabled", true); obj.val("resend(" + countdown + ")"); countdown--;editCookie("secondsremained",countdown,countdown+1);} setTimeout(function() { settime(obj) },1000) //Execute every 1000 milliseconds} //Check whether 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 phone number!'); return false; }else{return true;}}</script></html>The above is the countdown function after sending SMS verification code based on JS introduced by the editor (ignoring the page refresh, the page is closed and the countdown function is not performed). I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!