1 var InterValObj; //timer变量,控制时间 2 var count = 5; //间隔函数,1秒执行 3 var curCount;//当前剩余秒数 4 5 function sendMessage() { 6 curCount = count; 7 //设置button效果,开始计时 8 $("#btnSendCode").attr("disabled", "true"); 9 $("#btnSendCode").val("请在" + curCount + "秒内输入验证码");10 InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次11 //向后台发送处理数据12 $.ajax({13 type: "POST", //用POST方式传输14 dataType: "text", //数据格式:JSON15 url: 'Login.ashx', //目标地址16 data: "dealType=" + dealType +"&uid=" + uid + "&code=" + code,17 error: function (XMLHttpRequest, textStatus, errorThrown) { },18 success: function (msg){ }19 });20 }21 22 //timer处理函数23 function SetRemainTime() {24 if (curCount == 0) { 25 window.clearInterval(InterValObj);//停止计时器26 $("#btnSendCode").removeAttr("disabled");//启用按钮27 $("#btnSendCode").val("重新发送验证码");28 }29 else {30 curCount--;31 $("#btnSendCode").val("请在" + curCount + "秒内输入验证码");32 }33 }
转自:http://www.cnblogs.com/xcsn/archive/2013/04/22/3035240.html