Based on the JS implementation of SMS Verification code after the countdown function (ignoring the page refresh, the page is closed without the countdown function)

Source: Internet
Author: User

<!doctype html> "en"><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>--><body><input id="phonenum" type="text" value="18518181818"/><input id="second" type="button" value="免费获取验证码" /></body><script>//发送验证码时添加cookiefunction addCookie(name,value,expiresHours){ //判断是否设置过期时间,0代表关闭浏览器时失效if(expiresHours>0){ var date=new Date(); date.setTime(date.getTime()+expiresHours*1000); $.cookie(name, escape(value), {expires: date});}else{$.cookie(name, escape(value));}} //修改cookie的值function editCookie(name,value,expiresHours){ if(expiresHours>0){ var date=new Date(); date.setTime(date.getTime()+expiresHours*1000); //单位是毫秒$.cookie(name, escape(value), {expires: date});} else{$.cookie(name, escape(value));}} //根据名字获取cookie的值function getCookieValue(name){ return $.cookie(name);}$(function(){$("#second").click(function (){sendCode($("#second"));});v = getCookieValue("secondsremained");//获取cookie值if(v>0){settime($("#second"));//开始倒计时}})//发送验证码function sendCode(obj){var phonenum = $("#phonenum").val();var result = isPhoneNum();if(result){// doPostBack(‘${base}/login/getCode.htm‘,backFunc1,{"phonenum":phonenum});addCookie("secondsremained",60,60);//添加cookie记录,有效时间60ssettime(obj);//开始倒计时}}//开始倒计时var countdown;function settime(obj) { countdown=getCookieValue("secondsremained");if (countdown == 0) { obj.removeAttr("disabled"); obj.val("免费获取验证码"); return;} else { obj.attr("disabled", true); obj.val("重新发送(" + countdown + ")"); countdown--;editCookie("secondsremained",countdown,countdown+1);} setTimeout(function() { settime(obj) },1000) //每1000毫秒执行一次} //校验手机号是否合法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(‘请输入有效的手机号码!‘); return false; }else{return true;}}</script>

Based on the JS implementation of SMS Verification code after the countdown function (ignoring the page refresh, the page is closed without the countdown function)

Related Article

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.