日期:2014-05-16  浏览次数:20415 次

js 获取验证码倒计时
点击通过手机或邮箱获取验证码,点击之后进入手机验证码或者邮箱验证码获得的页面,验证码已经发出。需要在页面上出现倒计时,可参照如下代码。
  var wait=60;
	var logNum = Math.floor(Math.random() * 60 + 1);
	function time(o,num) {
			if (wait == 0) {
				o.removeAttribute("disabled");			
				o.value="再次免费获取验证码";
				wait = 60;
			} else {
				o.setAttribute("disabled", true);
				o.value="重新发送(" + wait + ")";
				if(wait==logNum){
					resend();
				}
				wait--;
				setTimeout(function() {time(o,wait);},1000);
			}
		}
	document.getElementById("btn").onclick=function(){time(this,null);}
	
	var maxtime = 60; 
	function CountDown(){  
		if(maxtime>=0){  
				document.all["btn"].setAttribute("disabled", true);
				seconds = Math.floor(maxtime%60);
				msg = "重新发送("+seconds+")";  
				document.all["btn"].value = msg;  
				--maxtime;  
		}  
		else{  
				clearInterval(timer); 
				msg = "重新发送";
				document.all["btn"].value = msg; 
				document.all["btn"].removeAttribute("disabled");
			}  
		}  
	timer = setInterval("CountDown()",1000);