<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>测试发送验证码倒计时</title>
<style>
*{
padding:0;
margin:0;
}
#getverification {
width: 100px;
line-height: 28px;
border-radius: 5px;
font-size: 12px;
font-family: "Microsoft YaHei";
text-align: center;
color: #fff;
background-color: #29A6DD;
cursor: pointer;
margin-left: 46%;
margin-top: 20%;
}
</style>
</head>
<body>
<button type="button" id="getverification">获取验证码</button>
<script type="text/javascript" src="lib/jquery.min.js"></script>
<script>
$('#getverification').on('click',function(){
var that = $(this);
var time = 60;
var timeStop = setInterval(function(){
time--;
if (time>0) {
that.text('重新发送' + time +'s');
that.attr('disabled','disabled');//禁止点击
}else{
time = 60;//当减到0时赋值为60
that.text('获取验证码');
clearInterval(timeStop);//清除定时器
that.removeAttr('disabled');//移除属性,可点击
}
},1000)
})
</script>
</body>
</html>