html结构:
<input type="button" id="btn1" value="开启"/><input type="button" id="btn2" value="关闭"/> js代码:
var oBtn1=document.getElementById(‘btn1‘);var oBtn2=document.getElementById(‘btn2‘);var timer=null; //timer先设为空对象;oBtn1.onclick= function () { timer= setInterval(function () { alert(‘ni‘); },1000);};oBtn2.onclick=function(){ clearInterval(timer);};
时间: 2024-10-24 02:32:15