0
点赞
收藏
分享

微信扫一扫

JS实现发送验证码倒计时

Android开发指南 2022-03-31 阅读 96
<a id="sendCode"> 发送验证码 </a>
$(function () {
    $("#sendCode").click(function () {
        //2、倒计时
        if ($(this).hasClass("disabled")) {
            //正在倒计时中
        } else {
            //1、给指定手机号发送验证码
            $.get("/sms/sendcode?phone=" + $("#phoneNum").val(), function (data) {
                if (data.code != 0) {
                    alert(data.msg);
                }
            });
            timeoutChangeStyle();
        }
    });
});
var num = 10;

function timeoutChangeStyle() {
    $("#sendCode").attr("class", "disabled");
    if (num == 0) {
        $("#sendCode").text("发送验证码");
        num = 5;
        $("#sendCode").attr("class", "");
    } else {
        var str = num + "s 后再次发送";
        $("#sendCode").text(str);
        setTimeout("timeoutChangeStyle()", 1000);
    }
    num--;
}
举报

相关推荐

0 条评论