定时器优化
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <input type="button" value="启动" onclick="time()"/> </body> </html> <script type="text/javascript"> var timeout = false; //启动及关闭按钮 function time() { if(timeout) return; Method(); setTimeout(time,1000); //time是指本身,延时递归调用自己,100为间隔调用时间,单位毫秒 } function Method(){ console.log(1); } </script>