案例:点击按钮后,该按钮60秒之内不能再次点击,防止重复发送短信
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title></title> 6 </head> 7 <body> 8 手机号码:<input type="number" /><button>发送</button> 9 <script> 10 var btn = document.querySelector('button'); 11 var time = 3; 12 btn.addEventListener('click', function(){ 13 btn.disabled = true; 14 var timer = setInterval(function() { 15 if (time == 0) { 16 // 清除定时器和复原按钮 17 clearInterval(timer); 18 btn.disabled = false; 19 btn.innerHTML = '发送'; 20 } else { 21 btn.innerHTML = '还剩下' + time + '秒'; 22 time--; 23 } 24 }, 1000); 25 }) 26 </script> 27 </body> 28 </html>