<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript"> window.onload=function(){ var send=document.getElementById('send'), second=document.getElementById('second'), times=5, timer=null; send.onclick = function () { // 计时开始 timer = setInterval(function () { autoPlay(); }, 1000) } //自动播放 function autoPlay(){ if (times <= 0){ times = 5; send.disabled=false; send.value = '发送验证码'; second.innerHTML=0; clearInterval(timer) return; } send.disabled=true; send.value = '正在发送...'; second.innerHTML=times; times--; } } </script> </head> <body> <input type="button" id="send" value="发送验证码"><button disabled><span id="second">0</span>秒后重试</button> </body> </html>
为什么现在只能写在function内了
timer = setInterval(autoPlay, 1000); //直接这么写的话autoPlay后面不能加()