用於一些註冊類的場景,點擊發送驗證碼,xx秒後從新發送。javascript
利用 setTimeout 方法,xx秒後執行指定的方法,修改button的屬性值,disabled爲true時爲灰色,不可點擊。html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!--<script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script>--> <script type="text/javascript"> var countdown=60; function settime(obj) { if (countdown == 0) { obj.removeAttribute("disabled"); obj.value="獲取驗證碼"; countdown = 60; return; } else { obj.setAttribute("disabled", true); obj.value="從新發送(" + countdown + ")"; countdown--; } setTimeout(function() { settime(obj); } ,1000); } </script> <body> <label> <input type="text" name="textfield"> </label> <input type="button" id="btn" value="獲取驗證碼" onclick="settime(this)" /> </body> </html>