廢話不多說,看代碼。
這是獲取短信的按鈕
<a href="javascript:void(0);" id="gSMS" onclick="get_sms_captcha('2')" style="font-size: 0.6rem;color: #444;left: 0.5rem;">獲取短信驗證碼</a>
下面是js代碼,思想就是定義60的數,然后定時器設置為一秒,每一秒執行一次。等到0是就可以重新獲取短信驗證碼。
var countDownT = 60;
function get_sms_captcha(type){
countDownT = 60;
setTime();
//下方寫業務
}
function setTime(){
if (countDownT == 0){
$("#gSMS").attr("onclick","get_sms_captcha('2')");
$("#gSMS").text("獲取短信驗證碼");
} else{
$("#gSMS").attr("onclick","#");
$("#gSMS").text("重新發送("+countDownT+")");
countDownT--;
setTimeout(function () {
setTime();
},1000)
}
}