var res_code; //后台返回的验证码,注册时验证用 var InterValObj; //timer变量,控制时间 var count = 60; //间隔函数,1秒执行
var curCount = 0; //当前剩余秒数 var authCode;
function getCode() { var iphoneNum = $('#userName').val(); //手机号 var prame = { 'mobilePhone': $('#userName').val() } if(iphoneNum == '') { alert('请填写手机号') } else { jQuery.ajax({ url: '$!webPath/send_code.htm', type: 'POST', data: prame, dataType: 'json', success: function(result) { if(result.flag == true) { sendMessage(); //doCompare(result.authCode); authCode = result.authCode; } else if(result.flag == false) { console.log('验证码发送失败') } } }) } }
function sendMessage() { curCount = count;
document.getElementById("getAuthcode").setAttribute("disabled", "disabled"); //设置按钮为禁用状态 document.getElementById("getAuthcode").setAttribute("style", "background-color:#F1F1F1;"); document.getElementById("getAuthcode").innerHTML = "(" + curCount + ")秒后再次获取"; InterValObj = window.setInterval(SetRemainTime, 1000); // 启动计时器timer处理函数,1秒执行一次 //SetRemainTime(); }
function SetRemainTime() { if(curCount == 0) { clearInterval(InterValObj); //停止计时器 document.getElementById("getAuthcode").removeAttribute("disabled"); //移除禁用状态改为可用 document.getElementById("getAuthcode").setAttribute("style", "background-color:#cc0000;"); document.getElementById("getAuthcode").innerHTML = "重新发送验证码"; } else { curCount--; document.getElementById("getAuthcode").innerHTML = "(" + curCount + ")秒后再次获取"; } }
function submit_login() { var userCode = $('#code').val(); if(userCode == null || userCode == '') { alert('请填写验证码') } else if(userCode == authCode) { jQuery('#theForm').submit(); } else { alert('验证码错误'); } }