Home  >  Article  >  Web Front-end  >  How to implement verification code countdown in JavaScript

How to implement verification code countdown in JavaScript

藏色散人
藏色散人Original
2021-07-03 09:35:352773browse

How to implement verification code countdown in JavaScript: first create an HTML sample file; then create a button; and finally pass "$('.hmac').on('click',function(){... }" method to obtain the verification code and count down for 60 seconds.

How to implement verification code countdown in JavaScript

The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, Dell G3 computer.

How to implement verification code countdown in JavaScript?

js gets verification code countdown 60s (super simple)

<!DOCTYPE html>
<html>
<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>获取验证码</button>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script>
$(&#39;.hmac&#39;).on(&#39;click&#39;,function(){
   var that = $(this)
    var timeo = 60;
    var timeStop = setInterval(function(){
        timeo--;
        if (timeo>0) {
            that.text(&#39;重新发送&#39; + timeo +&#39;s&#39;);
            that.attr(&#39;disabled&#39;,&#39;disabled&#39;);//禁止点击
        }else{
            timeo = 60;//当减到0时赋值为60
            that.text(&#39;获取验证码&#39;); 
            clearInterval(timeStop);//清除定时器
            that.removeAttr(&#39;disabled&#39;);//移除属性,可点击
        }
    },1000)
})
</script>
</body>
</html>

Recommended study: "javascript advanced tutorial

The above is the detailed content of How to implement verification code countdown in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn