js 短信60秒倒计时

废话不多说,看代码。

这是获取短信的按钮

<a href="javascript:void(0);" id="gSMS" onclick="get_sms_captcha('2')" style="font-size: 0.6rem;color: #444;left: 0.5rem;">获取短信验证码</a> 

下面是js代码,思想就是定义60的数,然后定时器设置为一秒,每一秒执行一次。等到0是就可以重新获取短信验证码。

var countDownT = 60;
function get_sms_captcha(type){
    countDownT = 60;
    setTime();
    //下方写业务
}

function  setTime(){
    if (countDownT == 0){
        $("#gSMS").attr("onclick","get_sms_captcha('2')");
        $("#gSMS").text("获取短信验证码");
    } else{
        $("#gSMS").attr("onclick","#");
        $("#gSMS").text("重新发送("+countDownT+")");
        countDownT--;
        setTimeout(function () {
            setTime();
        },1000)
    }
}

猜你喜欢

转载自www.cnblogs.com/zhengxq21/p/9689773.html