js發送驗證碼

第一種類型:js倒計時60秒,從新發送驗證碼


<script src="js/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">

    var InterValObj; //timer變量,控制時間
    var count = 60; //間隔函數,1秒執行
    var curCount;//當前剩餘秒數

    function sendMessage() {
        curCount = count;
        //設置button效果,開始計時
        $("#btnSendCode").attr("disabled", "true");
        var dAo =document.getElementById('btnSendCode').style.background="#dadada";
        $("#btnSendCode").val("倒計時" + curCount + "秒");
        InterValObj = window.setInterval(SetRemainTime, 1000); //啓動計時器,1秒執行一次
        //向後臺發送處理數據
        $.ajax({
            type: "POST", //用POST方式傳輸
            dataType: "text", //數據格式:JSON
            url: 'Login.ashx', //目標地址
            data: "dealType=" + dealType +"&uid=" + uid + "&code=" + code,
            error: function (XMLHttpRequest, textStatus, errorThrown) { },
            success: function (msg){ }
        });
    }

    //timer處理函數
    function SetRemainTime() {
        if (curCount == 0) {
            window.clearInterval(InterValObj);//中止計時器
            $("#btnSendCode").removeAttr("disabled");//啓用按鈕
            var dAo =document.getElementById('btnSendCode').style.background="#fff";
            $("#btnSendCode").val("從新發送驗證碼");
        }
        else {
            curCount--;
            var dAo =document.getElementById('btnSendCode').style.background="#dadada";
            $("#btnSendCode").val("倒計時" + curCount + "秒");
        }
    }
</script>

 

第二種:簡單的倒計時60秒javascript

<input type="button" id="btn" value="免費獲取驗證碼" />  前端

<script type="text/javascript">  java

var wait=60;  jquery

function time(o) {  ajax

     if (wait == 0) {  網絡

       o.removeAttribute("disabled");            函數

            o.value="免費獲取驗證碼";  優化

            wait = 60;  ui

        } else {  this

            o.setAttribute("disabled", true);  

           o.value="從新發送(" + wait + ")";  

           wait--;  

            setTimeout(function() {  

                time(o)  

            },  

            1000)  

        }  

    }  

document.getElementById("btn").onclick=function(){time(this);}  

</script>  

 

 

 

//本文來源於網絡,本人總結起來並優化,分享給有須要的前端朋友,若有侵權,請聯繫刪除,也請指正、批評

相關文章
相關標籤/搜索