簡訊驗證碼傳送後倒計時60秒實現做法
阿新 • • 發佈:2018-12-01
//驗證碼倒計時60秒 var countdown=60; function settime(obj) { //傳送驗證碼倒計時 if (countdown == 0) { obj.attr('disabled',false); //obj.removeattr("disabled"); obj.val("免費獲取驗證碼"); countdown = 60; return; } else { obj.attr('disabled',true); obj.html("重新發送(" + countdown + ")");//我html用的是button按鈕,如果使用input按鈕的話 ,把html(),方法換成val()方法 countdown--; } setTimeout(function() { settime(obj) } ,1000) }//傳送驗證碼 $("#sendCode").click(function () { //按鈕的單擊事件 var obj = $("#sendCode"); //獲取到按鈕物件調方法 settime(obj);});
html程式碼
<button id="sendCode" class="btn btn-primary" style="margin-top: 20px">點擊發送驗證碼</button>