定时器
开启定时器
Setinterval间隔型 每隔一段时间重复的执行
SetTimeout延时型 只执行一次
两种定时器的区别
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> function show(){ alert('a'); } setInterval(show,1000);//每隔1000毫秒弹出一个a </script> </head> <body> </body> </html>
停止定时器
ClearInterval
clearTimeout
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> window.onload=function(){ var oBtn1=document.getElementById('btn1'); var oBtn2=document.getElementById('btn2'); var timer=null; oBtn1.onclick=function(){ timer=setInterval(function(){ alert('a'); },1000); }; oBtn2.onclick=function(){ clearInterval(timer); } }; </script> </head> <body> <button> <input id="btn1" type="button" value="开始"/> <input id="btn2" tyPe="button" value="结束"/> </button> </body> </html>