jquery 手机获取验证码计时

时间:2023-03-08 17:44:33

html:

<input type="text" class="codeText" id="txtverifycode" /> <button id="btn1" class="btn code" onclick="GetVerifycode(); " >获取验证码
                    </button>
jquery:
 <script type="text/javascript">

var iTime = 60;
 //获取验证码
        function GetVerifycode() {
            var t;
            if (iTime == 60) {
                telphone = $("#txttelphone").val();
                // var isMobile = /^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/;
                // if (!isMobile.test(telphone)) {
                if (!(/^1[3|4|5|7|8]\d{9}$/.test(telphone))) {
                    alert("手机号码格式不正确!");
                    $("#txttelphone").focus();
                    return false;
                }
                $.ajax({
                    type: "GET",
                    url: "http://www.aosmith.com.cn/webService/Getverifycode.ashx?jsoncallback=?",
                    dataType: "jsonp",
                    jsonp: "jsoncallback",
                    data: {
                        "telphnoe": telphone
                    },
                    success: function (data) {
                        if (data.result == "0") {
                            reply_time = data.reply_time;
                            verifycode = data.verifycode;
                        }
                        else {
                            alert("获取失败!");
                            $(".GetCode-active").show();
                            $(".sharebg-active").show();
                            // alert(data.result);
                        }
                    }
                });
            }
            
            $("#btn1").attr("disabled", true);  //页面加载后禁用按钮
            iTime = iTime - 1;
            $("#btn1").html('验证码发送中(' + iTime + ')');
            t = setTimeout('GetVerifycode()', 1000); //1秒后启用             if (iTime == 0) {
                clearTimeout(t);
                iTime =60;
                $("#btn1").val('重新发送验证码');
                $("#btn1").attr("disabled", false); //页面加载后禁用按钮             }
        };

</script>