設定網頁一定時間後重新發送驗證碼(60秒)
阿新 • • 發佈:2018-12-19
這裡廢話不多說直接程式碼提供參考:
var InterValObj; //timer變數,控制時間
var count = 60; //間隔函式,1秒執行
var curCount;//當前剩餘秒數
//傳送手機驗證碼
function sendPhoneCode(){
curCount = count;
//設定button效果,開始計時
//$("#btnSendCode").attr("disabled", "true");
$("#btnSendCode").text(curCount + "秒後重新發送");
$("#btnSendCode").removeAttr("onclick" );
InterValObj = window.setInterval(SetRemainTime, 1000); //啟動計時器,1秒執行一次
if(checkPhoneNum()){ //檢查手機號格式
$.ajax({
url:"地址",
type : 'POST',
data : {
"user":JSON.stringify({phone_num:$("#phone_num").val(),}),
},
dataType : "json" ,
success : function(data) {
layer.msg(data.msg, {icon:data.type});
}
})
}
}
//timer處理函式
function SetRemainTime() {
if (curCount == 1) {
window.clearInterval(InterValObj);//停止計時器
//$("#btnSendCode").removeAttr("disabled");//啟用按鈕
$("#btnSendCode").attr("onclick","sendPhoneCode();");
$("#btnSendCode").text("免費獲取");
}
else {
curCount--;
$("#btnSendCode").text(curCount + "秒後重新發送");
}
}
//驗證手機號是否為空
function checkPhoneNum(){
var phone_num = $("#phone_num").val();
if(phone_num==""){
layer.msg("請輸入您的手機號碼",{icon:0});
return false;
}
return true;
}