在同一页面,如果不通过clearInterval()清除的话,if条件语句的判断都是无效的,会一直定时执行
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>漏刻有时数据可视化组件开发实例</title> <script type="text/javascript" src="js/jquery.js"></script> </head> <body> <input type="button" value="开始" id="oBtn1"> <input type="button" value="结束" id="oBtn2"> <script> var oBtn1 = document.getElementById("oBtn1"); var oBtn2 = document.getElementById("oBtn2"); var timer; oBtn1.onclick = function () { clearInterval(timer); timer = setInterval(function () { console.log("index"); }, 10); } oBtn2.onclick = function () { clearInterval(timer); } </script> </body> </html>
Done!