jquery 60s倒數計時

青青子衿619發表於2018-07-13

前端開發中經常用到的傳送按鈕倒數計時,每次都是重寫,挺麻煩的,記錄一下,以後直接來複制程式碼

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no" id="viewport" name="viewport">
        <title>60s倒數計時</title>
        <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <input type="button" value="郵箱驗證碼" onclick="sendVerificationCode(this)" />
            
        <script type="text/javascript">
            //傳送驗證碼按鈕的60s倒數計時
            var countdown = 60;
            function sendVerificationCode(obj) {
                if(countdown == 0) {
                    obj.removeAttribute("disabled");
                    obj.value = "重新獲取";
                    countdown = 60;
                    return;
                } else {
                    obj.setAttribute("disabled", true);
                    obj.value = "重新(" + countdown + ")";
                    countdown--;
                }
                setTimeout(function() {
                    sendVerificationCode(obj)
                }, 1000)
            }
        </script>
    </body>
</html>

 

相關文章