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

//验证码倒计时60秒
var countdown=60;
function settime(obj) { //发送验证码倒计时
    if (countdown == 0) {
        obj.attr('disabled',false);
        //obj.removeattr("disabled");
        obj.val("免费获取验证码");
        countdown = 60;
        return;
    } else {
        obj.attr('disabled',true);
        obj.html("重新发送(" + countdown + ")");//我html用的是button按钮,如果使用input按钮的话 ,把html(),方法换成val()方法                        
        countdown--;
    }
    setTimeout(function() {
            settime(obj) }
        ,1000)
}
//发送验证码
$("#sendCode").click(function () {  //按钮的单击事件
    var obj = $("#sendCode");    //获取到按钮对象调方法
    settime(obj);

});

html代码

<button id="sendCode" class="btn btn-primary" style="margin-top: 20px">点击发送验证码</button>

猜你喜欢

转载自blog.csdn.net/kxj19980524/article/details/84614966