<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script src="jquery-1.8.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
var InterValObj; //timer变量,控制时间
var count = 120; //间隔函数,1秒执行
var curCount;//当前剩余秒数
var codeLength = 6;//验证码长度
var yzm = '';
function sendMessage() {
curCount = count;
var phone='13570339063';//手机号码
var CertType = "00";
if(phone != ""){
//设置button效果,开始计时
$("#btnSendCode").removeAttr("onclick");//去掉onclick
$("#btnSendCode").val(curCount+"s");
InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次
//向后台发送处理数据
$.ajax({
type: "GET", //用POST方式传输
dataType: "json", //数据格式:JSON
url: '/binding/sendCode', //目标地址
data : {
phone : phone,
certType : CertType
},
success : function(data) {
var message = decodeURI(data.message);
yzm = decodeURI(data.yzm);
if (message)
alert(message+"验证码:"+yzm);
},
error : function() {
alert('短信验证码发送失败,请重新发送');
}
});
}else{
alert("手机号码不能为空!");
}
}
//timer处理函数
function SetRemainTime() {
if (curCount == 0) {
window.clearInterval(InterValObj);//停止计时器
$("#btnSendCode").attr("onclick","sendMessage()");//启用按钮
$("#btnSendCode").text("重新发送验证码");
}
else {
curCount--;
$("#btnSendCode").text(curCount+"s");
}
}
</script>
</head>
<body>
<a id="btnSendCode" onclick="sendMessage()">获取验证码</a>
</body>
</html>