当我们实现手机短信验证码功能时,为了防止用户多次点击“获取验证码”按钮,这时需要将按钮设置为倒计时(倒计时中不能点击)。
jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script> <!-- 菜鸟课程 -->
<script type="text/javascript">
var countdown=5; //自定义倒计时秒数
function sendemail(){
var obj = $("#btn");
settime(obj);
}
function settime(obj) { //开启验证码倒计时
if (countdown == 0) {
obj.attr('disabled',false);
//obj.removeattr("disabled");
obj.val("获取验证码");
countdown = 5;
return;
} else {
obj.attr('disabled',true);
obj.val("重新发送(" + countdown + ")");
countdown--;
}
setTimeout(function() {
settime(obj) }
,1000)
}
</script>
<body>
<input type="button" id="btn" value="获取验证码" onclick="sendemail()" />
</body>
</html>