JavaScript實現驗證碼倒計時的方法:首先創建一個HTML示例文件;然后創建一個button;最后通過“$('.hmac').on('click',function(){…}”方法獲取驗證碼并實現倒計時60秒即可。
本文操作環境:windows7系統、javascript1.8.5版、Dell G3電腦。
JavaScript怎么實現驗證碼倒計時?
js獲取驗證碼倒計時60s(超簡單)
<!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> $('.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>
推薦學習:《javascript高級教程》