<!DOCTYPE html> <html> <head> <title></title> </head> <body> <button id="start">开始</button> <button id="close">结束</button> <script type="text/javascript"> //js定时器分为重复执行与一次执行。 /*let startBtn = document.querySelector("#start"); let id=null; startBtn.οnclick=function() { id=setInterval(function() { console.log("随便写点"); },1000); } let closeBtn=document.querySelector("#close"); closeBtn.οnclick=function() { clearInterval(id); } */ //以上重复。以下一次 let startBtn = document.querySelector("#start"); let closeBtn = document.querySelector("#close"); let id = null; startBtn.onclick=function() { id=window.setTimeout(function() { console.log("随便写点"); },2000); } closeBtn.onclick=function() { clearTimeout(id); } </script> </body> </html>
dom啦9 定时器大详解
关注
打赏