js实现发送验证码倒计时按钮

在写注册页面时,有时候需要发送注册邮件来获取验证码,但是为了防止多次重复发送邮件,

必须让按钮隔一段时间后才能再次使用。

 

代码如下:

 1 <html> 
 2 <head>
 3 <meta charset="utf-8">
 4 <title>验证码按钮</title> 
 5 </head>
 6 <body>
 7     <button type="button" id="send">发送验证码</button>
 8     <script type="text/javascript">
 9         wait = 60;
10         function wait_time(btn) {
11             if (wait == 0) {
12                 btn.removeAttribute("disabled");
13                 btn.innerText = "发送验证码";
14                 wait = 60;
15             } else {
16                 btn.setAttribute("disabled", true);
17                 btn.innerText = wait+"秒后重新发送";
18                 wait--;
19                 setTimeout(function(){wait_time(btn);},1000);
20             }
21         }
22     document.getElementById('send').onclick = function(){wait_time(this)};
23     </script>
24 </body> 
25 </html> 

 

posted @ 2016-09-28 17:44  菜豆客  阅读(211)  评论(0编辑  收藏  举报