实现发送短信验证码后60秒倒计时

方法①:

<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>60s</title>
</head>
<script type="text/javascript"> 
var countdown=60; 
function settime(obj) { 
    if (countdown == 0) { 
        obj.removeAttribute("disabled");    
        obj.value="获取验证码"; 
        countdown = 60; 
        return;
    } else { 
        obj.setAttribute("disabled", true); 
        obj.value="重新发送(" + countdown + ")"; 
        countdown--; 
    } 
setTimeout(function() { 
    settime(obj) }
    ,1000) 
}
</script>
<body> 
<input type="button" id="btn" value="获取验证码" onclick="settime(this)" /> 
    </body>

</html>

方法②:JQuery实现

注:别忘记在js包中将jquery.min.js放进去生气

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/jquery.min.js"></script>
<script type="text/javascript"> 
function invokeSettime(obj){
    var countdown=60;
    settime(obj);
    function settime(obj) {
        if (countdown == 0) {
            $(obj).attr("disabled",false);
            $(obj).text("获取验证码");
            countdown = 60;
            return;
        } else {
            $(obj).attr("disabled",true);
            $(obj).text("(" + countdown + ")s 重新发送");
            countdown--;
        }
        setTimeout(function() {
                    settime(obj) }
                ,1000)
    }
}
</script>
</head>
<body>
<button id="btn" type="button" onclick="invokeSettime(this)">获取验证码</button>
</body>
</html>




猜你喜欢

转载自blog.csdn.net/weixin_39665076/article/details/79770281