JS實現驗證碼倒計時效果

一般作註冊頁面時會有獲取驗證碼按鈕,而後點擊後過60秒才能從新獲取,好比如今項目中遇到的css

而後點擊後的樣式,而且數字是遞減的,到0時從新回到最初的狀態(上圖)。this

首先構造HTML結構spa

<button class="getCode">獲取驗證碼</button>

 

css就略了code

JS實現:blog

var wait=60;
        function time(o){
            if (wait==0) {
                o.removeAttribute("disabled");    
                o.innerHTML="輸入驗證碼";
                o.style.backgroundColor="#fe9900";
                wait=60;
            }else{
                o.setAttribute("disabled", true);
                o.innerHTML=wait+"秒後從新獲取";
                o.style.backgroundColor="#8f8b8b";
                wait--;
                setTimeout(function(){
                    time(o)
                },1000)
            }
        }

最後點擊按鈕,調用time方法:rem

$(".getCode").click(function(){
            time(this);
        });

 

至此所有功能所有完畢。get

相關文章
相關標籤/搜索