發送短信驗證碼倒計時&&防止刷新
阿新 • • 發佈:2019-02-22
tst ext fine 防刷新 button coo 清除 fin 不可 html代碼
//在發送按鈕顯示剩余倒計時
$(‘#btn‘).val(‘請等待‘+total+‘秒‘);
//把剩余總倒計時減掉1
total--;
if(total==0){//剩余倒計時為零,則顯示 重新發送,可點擊
//清除定時器
clearInterval(interval);
//刪除cookie
total=$.cookie("total",total, { expires: -1 });
//顯示重新發送
$(‘#btn‘).val(‘重新發送‘);
//把發送按鈕設置為可點擊
$(‘#btn‘).attr("disabled", false);
}else{//剩余倒計時不為零
//重新寫入總倒計時
$.cookie("total",total);
}
},1000);
} timekeeping();
}else{
$(‘#btn‘).attr("disabled", false);
}
});
</script>
<input type="button" id="btn" class="sms-btn" onclick="sendPh()" value=‘獲取驗證碼‘ />
js代碼 倒計時
function timekeeping(){
//把按鈕設置為不可以點擊
$(‘#btn‘).attr("disabled", true);
var interval=setInterval(function(){//每秒讀取一次cookie
//從cookie 中讀取剩余倒計時
total=$.cookie("total"); 存入Cookie 防止頁面刷新
//在發送按鈕顯示剩余倒計時
$(‘#btn‘).val(‘請等待‘+total+‘秒‘);
//把剩余總倒計時減掉1
total--;
if(total==0){//剩余倒計時為零,則顯示 重新發送,可點擊
//清除定時器
clearInterval(interval);
//刪除cookie
total=$.cookie("total",total, { expires: -1 });
//顯示重新發送
$(‘#btn‘).val(‘重新發送‘);
//把發送按鈕設置為可點擊
$(‘#btn‘).attr("disabled", false);
}else{//剩余倒計時不為零
//重新寫入總倒計時
$.cookie("total",total);
},1000);
}
function sendPh(){
phone=$(‘#mobile_phone‘).val();
$.post(phone_url, {mobile:phone}, function(data, textStatus, xhr) {
$.cookie("total",60);
timekeeping();
});
}
防刷新 html中加入js
<script type="text/javascript">
$(function(){
if($.cookie("total")!=undefined&&$.cookie("total")!=‘NaN‘&&$.cookie("total")!=‘null‘){
}else{
$(‘#btn‘).attr("disabled", false);
}
});
</script>
發送短信驗證碼倒計時&&防止刷新