JavaWeb手机短信实现前台利用JS获取随机验证码,倒计时效果
1 var random=""; 2 function getMessages(button){ 3 //前台倒计时 4 front(button); 5 //请求后台发送验证码 6 behind(); 7 } 8 9 ///后台发送短信 10 function behind (){ 11 //随机码 12 random=getRandom(); 13 var tel=document.getElementById("tel").value; 14 $.post("<%=path%>/sendSmsServlet.action",{tel:tel,random:random},function(data){ 15 16 }); 17 } 18 19 //前台实现倒计时 20 function front(button){ 21 //前台倒计时 22 var count = 60; 23 button.value = count + "s后可重新获取验证码"; 24 //将按钮设置为不可点击 25 button.setAttribute("disabled", "disabled"); 26 var time = setInterval(function() { 27 count--; 28 button.value = count + "s后可重新获取验证码"; 29 if (count <= 0) { 30 //关闭定时器 31 clearInterval(time); 32 //改变按钮标题 33 button.value = "获取验证码"; 34 button.removeAttribute("disabled"); 35 } 36 }, 1000); 37 } 38 39 //获取四位随机完整码 40 function getRandom(){ 41 var str=""; 42 for(var i=0;i<4;i++){ 43 var num=parseInt(Math.random()*1000000000%10); 44 str+=num; 45 } 46 return str; 47 } 48