js獲取驗證碼倒數計時60s(超簡單)

lily000000發表於2018-01-04
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>測試jquey的ajax方法</title>
    <style>
        *{
            padding:0;
            margin:0;
        }
        button{width: 20%;line-height: 50px;font-size: 30px;margin-left: 40%;margin-top: 20%;  }
    </style>
</head>
<body>
<div id="test"></div>
<button class="hmac">獲取驗證碼</button>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script>
$('.hmac').on('click',function(){
   var that = $(this)
    var timeo = 60;
    var timeStop = setInterval(function(){
        timeo--;
        if (timeo>0) {
            that.text('重新傳送' + timeo +'s');
            that.attr('disabled','disabled');//禁止點選
        }else{
            timeo = 60;//當減到0時賦值為60
            that.text('獲取驗證碼'); 
            clearInterval(timeStop);//清除定時器
            that.removeAttr('disabled');//移除屬性,可點選
        }
    },1000)
})
</script>
</body>
</html>
複製程式碼

相關文章