js倒數計時 實現傳送驗證碼倒數計時60s

零love發表於2018-01-20
<script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script>
<script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<button type="button" style="outline:none;" id="code">點選傳送驗證碼</button>

<script>
countdown($('#code'));
function countdown($code) {
        var btn = $code;
        var count = 60;
        var resend = setInterval(function(){
            count--;
            if (count > 0){
                btn.html("倒數計時"+count);
                $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});
            }else {
                clearInterval(resend);
                btn.html("獲取驗證碼").removeAttr('disabled style');
            }
        }, 1000);
        btn.attr('disabled',true).css('cursor','not-allowed');
    }
</script>

相關文章