<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<title>获取验证码倒计时</title>
</head>
<body>
<button class="getCode">获取验证码</button>
</body>
<script>
var time;
$('.getCode').click(function(){
time = 10;
$('.getCode').html(`${time}s`)
$('.getCode').attr("disabled",true);
var a = setInterval(function (){
if(time !=0){
time --
$('.getCode').html(`${time}s`)
} else {
clearInterval(a)
$('.getCode').html(`重新发送`)
$('.getCode').attr("disabled",false);
}
},1000)
})
</script>
<!-- <script>
var time;
var flag = true;
$(".getCode").click(function get(){
if(flag) {
flag = false;
time = 10;
$('.getCode').html(`${time}s`);
var a = setInterval(function(){
if(time != 0) {
time--;
console.log(time)
$('.getCode').html(`${time}s`);
} else {
clearInterval(a);
$('.getCode').html('重新发送');
flag = true;
}
},1000)
}
})
</script> -->
</html>